From 255b6a0ef2f488a2fad051361699cc67db57338c Mon Sep 17 00:00:00 2001 From: Sami Haahtinen Date: Sun, 26 Oct 2025 21:21:41 +0000 Subject: [PATCH] home-manager-auto-upgrade: add flake support (#8053) Adds the ability for the auto-upgrade service to update a Nix flake instead of Nix channels. This is controlled by a new `useFlake` boolean option. When enabled, the service will run `nix flake update` in the directory specified by the `flakeDir` option. `flakeDir` defaults to the standard Home Manager configuration directory (`~/.config/home-manager`), making this feature work out-of-the-box for most users. The path is passed to the upgrade script via an environment variable in the systemd service unit. --- .../misc/news/2025/10/2025-10-25_15-45-39.nix | 12 +++++ .../services/home-manager-auto-upgrade.nix | 47 ++++++++++++++++--- .../home-manager-auto-upgrade/default.nix | 1 + .../flake-configuration.nix | 17 +++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 modules/misc/news/2025/10/2025-10-25_15-45-39.nix create mode 100644 tests/modules/services/home-manager-auto-upgrade/flake-configuration.nix diff --git a/modules/misc/news/2025/10/2025-10-25_15-45-39.nix b/modules/misc/news/2025/10/2025-10-25_15-45-39.nix new file mode 100644 index 000000000..fd0adac8c --- /dev/null +++ b/modules/misc/news/2025/10/2025-10-25_15-45-39.nix @@ -0,0 +1,12 @@ +{ + time = "2025-10-25T14:45:39+00:00"; + condition = true; + message = '' + The home-manager auto-upgrade service now supports updating Nix flakes. + + Enable this by setting `services.home-manager.autoUpgrade.useFlake = true;`. + + The flake directory can be configured with `services.home-manager.autoUpgrade.flakeDir`, + which defaults to the configured XDG config home (typically `~/.config/home-manager`). + ''; +} diff --git a/modules/services/home-manager-auto-upgrade.nix b/modules/services/home-manager-auto-upgrade.nix index 31a85c45d..41cea2be2 100644 --- a/modules/services/home-manager-auto-upgrade.nix +++ b/modules/services/home-manager-auto-upgrade.nix @@ -15,12 +15,28 @@ let autoUpgradeApp = pkgs.writeShellApplication { name = "home-manager-auto-upgrade"; - text = '' - echo "Update Nix's channels" - nix-channel --update - echo "Upgrade Home Manager" - home-manager switch - ''; + text = + if cfg.useFlake then + '' + set -e + if [[ ! -f "$FLAKE_DIR/flake.nix" ]]; then + echo "No flake.nix found in $FLAKE_DIR." >&2 + exit 1 + fi + echo "Changing to flake directory $FLAKE_DIR" + cd "$FLAKE_DIR" + echo "Update flake inputs" + nix flake update + echo "Upgrade Home Manager" + home-manager switch --flake . + '' + else + '' + echo "Update Nix's channels" + nix-channel --update + echo "Upgrade Home Manager" + home-manager switch + ''; runtimeInputs = with pkgs; [ homeManagerPackage nix @@ -47,6 +63,20 @@ in {manpage}`systemd.time(7)`. ''; }; + + useFlake = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Whether to use 'nix flake update' instead of 'nix-channel --update'."; + }; + + flakeDir = lib.mkOption { + type = lib.types.str; + default = "${config.xdg.configHome}/home-manager"; + defaultText = lib.literalExpression ''"''${config.xdg.configHome}/home-manager"''; + example = "/home/user/dotfiles"; + description = "The directory of the flake to update."; + }; }; }; @@ -70,7 +100,10 @@ in services.home-manager-auto-upgrade = { Unit.Description = "Home Manager upgrade"; - Service.ExecStart = "${autoUpgradeApp}/bin/home-manager-auto-upgrade"; + Service = { + ExecStart = "${autoUpgradeApp}/bin/home-manager-auto-upgrade"; + Environment = lib.mkIf cfg.useFlake [ "FLAKE_DIR=${cfg.flakeDir}" ]; + }; }; }; }; diff --git a/tests/modules/services/home-manager-auto-upgrade/default.nix b/tests/modules/services/home-manager-auto-upgrade/default.nix index f6bdb6c0e..5dca9feda 100644 --- a/tests/modules/services/home-manager-auto-upgrade/default.nix +++ b/tests/modules/services/home-manager-auto-upgrade/default.nix @@ -2,4 +2,5 @@ lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux { home-manager-auto-upgrade-basic-configuration = ./basic-configuration.nix; + home-manager-auto-upgrade-flake-configuration = ./flake-configuration.nix; } diff --git a/tests/modules/services/home-manager-auto-upgrade/flake-configuration.nix b/tests/modules/services/home-manager-auto-upgrade/flake-configuration.nix new file mode 100644 index 000000000..19b2eb181 --- /dev/null +++ b/tests/modules/services/home-manager-auto-upgrade/flake-configuration.nix @@ -0,0 +1,17 @@ +{ + services.home-manager.autoUpgrade = { + enable = true; + frequency = "daily"; + useFlake = true; + flakeDir = "/tmp/my-flake"; + }; + + nmt.script = '' + serviceFile="home-files/.config/systemd/user/home-manager-auto-upgrade.service" + assertFileExists "$serviceFile" + assertFileRegex "$serviceFile" "FLAKE_DIR=/tmp/my-flake" + + scriptPath=$(grep -oP 'ExecStart=\K.+' "$TESTED/$serviceFile") + assertFileRegex "$scriptPath" "nix flake update" + ''; +}