From c98173ce23ad5323ad7188caa10b06866341b489 Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Sun, 15 Dec 2024 22:04:06 +0100 Subject: [PATCH] Hyprland portal package fix. Added autoupgrade service --- nixos/configuration.nix | 33 +++---- .../modules/services/autoupgrade/default.nix | 90 +++++++++++++++++++ .../autoupgrade/nixos-upgrade-script.sh | 90 +++++++++++++++++++ 3 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 nixos/modules/services/autoupgrade/default.nix create mode 100755 nixos/modules/services/autoupgrade/nixos-upgrade-script.sh diff --git a/nixos/configuration.nix b/nixos/configuration.nix index b26efad..8223ca7 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -3,19 +3,29 @@ # and in the NixOS manual (accessible by running ‘nixos-help’). { config, lib, pkgs, stylix, hwconfig, username, nixvim, inputs, ... }: - { imports = [ # Include the results of the hardware scan. inputs.home-manager.nixosModules.default inputs.nixvim.nixosModules.nixvim ./firefox.nix + ./modules/services/autoupgrade ]; + kylekrein.services.autoUpgrade = { + enable = true; + pushUpdates = if hwconfig.hostname == "${username}-homepc" then true else false; + configDir = "/home/${username}/nixos-config"; + user = username; + }; boot = { plymouth = { enable = true; }; + loader = { + systemd-boot.enable = true; + efi.canTouchEfiVariables = if hwconfig.hostname != "${username}-mac" then true else false; + }; # Enable "Silent Boot" consoleLogLevel = 0; initrd.verbose = false; @@ -33,9 +43,6 @@ # It will just not appear on screen unless a key is pressed loader.timeout = 0; }; - # Bootloader. - boot.loader.systemd-boot.enable = true; - boot.loader.efi.canTouchEfiVariables = if hwconfig.hostname != "${username}-mac" then true else false; networking.hostName = hwconfig.hostname; # networking.wireless.enable = true; # Enables wireless support via wpa_supplicant. @@ -246,21 +253,6 @@ # This fixes the unpopulated MIME menus environment.etc."/xdg/menus/plasma-applications.menu".text = builtins.readFile "${pkgs.kdePackages.plasma-workspace}/etc/xdg/menus/plasma-applications.menu"; environment.etc."/xdg/menus/applications.menu".text = builtins.readFile "${pkgs.kdePackages.plasma-workspace}/etc/xdg/menus/plasma-applications.menu"; - #xdg.portal = { - # enable = true; - # config = { - # hyprland = { - # default = [ - # "hyprland" - # "kde" - # ]; - # }; - # }; - # configPackages = with pkgs; [ - # xdg-desktop-portal-hyprland - # kdePackages.xdg-desktop-portal-kde - # ]; - #}; environment.pathsToLink = [ "share/thumbnailers" ]; @@ -390,6 +382,7 @@ programs.hyprland = { enable = true; package = inputs.hyprland.packages."${hwconfig.system}".hyprland; + portalPackage = inputs.hyprland.packages.${hwconfig.system}.xdg-desktop-portal-hyprland; xwayland.enable = true; systemd.setPath.enable = true; }; @@ -406,6 +399,8 @@ settings = { experimental-features = ["nix-command" "flakes"]; auto-optimise-store = true; + substituters = ["https://hyprland.cachix.org"]; + trusted-public-keys = ["hyprland.cachix.org-1:a7pgxzMz7+chwVL3/pzj6jIBMioiJM7ypFP8PwtkuGc="]; }; }; } diff --git a/nixos/modules/services/autoupgrade/default.nix b/nixos/modules/services/autoupgrade/default.nix new file mode 100644 index 0000000..2b0ad8e --- /dev/null +++ b/nixos/modules/services/autoupgrade/default.nix @@ -0,0 +1,90 @@ +# Run automatic updates. Replaces system.autoUpgrade. +{ + config, + lib, + ... +}: + +let + cfg = config.kylekrein.services.autoUpgrade; + script = ./nixos-upgrade-script.sh; +in +{ + options = { + kylekrein.services.autoUpgrade = { + enable = lib.mkEnableOption "Enables automatic system updates."; + configDir = lib.mkOption { + type = lib.types.str; + description = "Path where your NixOS configuration files are stored."; + }; + extraFlags = lib.mkOption { + type = lib.types.str; + description = "Extra flags to pass to nixos-rebuild."; + default = ""; + }; + onCalendar = lib.mkOption { + default = "daily"; + type = lib.types.str; + description = "How frequently to run updates. See systemd.timer(5) and systemd.time(7) for configuration details."; + }; + operation = lib.mkOption { + type = lib.types.enum [ + "boot" + "switch" + "test" + ]; + default = "switch"; + description = "Which `nixos-rebuild` operation to perform. Defaults to `switch`."; + }; + persistent = lib.mkOption { + default = true; + type = lib.types.bool; + description = "If true, the time when the service unit was last triggered is stored on disk. When the timer is activated, the service unit is triggered immediately if it would have been triggered at least once during the time when the timer was inactive. This is useful to catch up on missed runs of the service when the system was powered down."; + }; + pushUpdates = lib.mkEnableOption "Updates the flake.lock file and pushes it back to the repo."; + user = lib.mkOption { + type = lib.types.str; + description = "The user who owns the configDir."; + }; + }; + }; + + config = lib.mkIf cfg.enable { + # Assert that system.autoUpgrade is not also enabled + assertions = [ + { + assertion = !config.system.autoUpgrade.enable; + message = "The system.autoUpgrade option conflicts with this module."; + } + ]; + + # Pull and apply updates. + systemd = { + services."nixos-upgrade" = { + serviceConfig = { + Type = "oneshot"; + User = "root"; + }; + #path = config.kylekrein.corePackages; + unitConfig.RequiresMountsFor = cfg.configDir; + script = + "${script} --operation ${cfg.operation} " + + (if (cfg.configDir != "") then "--flake ${cfg.configDir} " else "") + + (if (cfg.user != "") then "--user ${cfg.user} " else "") + + (if (cfg.pushUpdates) then "--update " else "") + + (if (cfg.extraFlags != "") then cfg.extraFlags else ""); + }; + timers."nixos-upgrade" = { + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = cfg.onCalendar; + Persistent = cfg.persistent; + Unit = "nixos-upgrade.service"; + RandomizedDelaySec = "30m"; + }; + }; + }; + }; +} diff --git a/nixos/modules/services/autoupgrade/nixos-upgrade-script.sh b/nixos/modules/services/autoupgrade/nixos-upgrade-script.sh new file mode 100755 index 0000000..1447d6c --- /dev/null +++ b/nixos/modules/services/autoupgrade/nixos-upgrade-script.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# Wrapper script for nixos-rebuild + +# Configuration parameters +operation="switch" # The nixos-rebuild operation to use +hostname=$(/run/current-system/sw/bin/hostname) # The name of the host to build +flakeDir="${FLAKE_DIR}" # Path to the flake file (and optionally the hostname). Defaults to the FLAKE_DIR environment variable. +update=false # Whether to update flake.lock (false by default) +user=$(/run/current-system/sw/bin/whoami) # Which user account to use for git commands (defaults to whoever called the script) +remainingArgs="" # All remaining arguments that haven't yet been processed (will be passed to nixos-rebuild) + +function usage() { + echo "nixos-rebuild Operations Script (NOS) updates your system and your flake.lock file by pulling the latest versions." + echo "" + echo "Running the script with no parameters performs the following operations:" + echo " 1. Pull the latest version of the config" + echo " 2. Update your flake.lock file" + echo " 3. Commit any changes back to the repository" + echo " 4. Run 'nixos-rebuild switch'." + echo "" + echo "Advanced usage: nixos-upgrade-script.sh [-o|--operation operation] [-f|--flake path-to-flake] [extra nixos-rebuild parameters]" + echo "Options:" + echo " -h, --help Show this help screen." + echo " -o, --operation The nixos-rebuild operation to perform." + echo " -f, --flake The path to your flake.nix file (and optionally, the hostname to build)." + echo " -U, --update Update and commit flake.lock." + echo " -u, --user Which user account to run git commands under." + echo "" + exit 2 +} + +# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash +POSITIONAL_ARGS=() +while [[ $# -gt 0 ]]; do + case "$1" in + --flake|-f) + flakeDir="$2" + shift + shift + ;; + --update|--upgrade|-U) + update=true + shift + ;; + --operation|-o) + operation="$2" + shift + shift + ;; + --user|-u) + user="$2" + shift + shift + ;; + --help|-h) + usage + exit 0 + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift + ;; + esac +done +remainingArgs=${POSITIONAL_ARGS[@]} +set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters + +if [ -z "${flakeDir}" ]; then + echo "Flake directory not specified. Use '--flake ' or set \$FLAKE_DIR." + exit 1 +fi + +cd $flakeDir + +echo "Pulling the latest version of the repository..." +/run/wrappers/bin/sudo -u $user git pull + +if [ $update = true ]; then + echo "Updating flake.lock..." + /run/wrappers/bin/sudo -u $user nix flake update --commit-lock-file && /run/wrappers/bin/sudo -u $user git push +else + echo "Skipping 'nix flake update'..." +fi + +options="--flake $flakeDir $remainingArgs --use-remote-sudo --log-format multiline-with-logs" + +echo "Running this operation: nixos-rebuild $operation $options" +/run/wrappers/bin/sudo -u root /run/current-system/sw/bin/nixos-rebuild $operation $options + +exit 0