1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

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.
This commit is contained in:
Sami Haahtinen 2025-10-26 21:21:41 +00:00 committed by GitHub
parent 2a9969b39c
commit 255b6a0ef2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 70 additions and 7 deletions

View file

@ -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}" ];
};
};
};
};