From 4e97102bd44899c514411aa8e604ecc33a9da356 Mon Sep 17 00:00:00 2001 From: Foxocube Date: Thu, 31 Jul 2025 18:41:29 +0100 Subject: [PATCH] nh: add options for specific flakes (#7566) * nh: Add options for specific flakes * nh: Add tests for specific flake options --------- Co-authored-by: Foxocube --- modules/programs/nh.nix | 85 ++++++++++++++++++--- tests/modules/programs/nh/darwin/config.nix | 6 ++ tests/modules/programs/nh/linux/config.nix | 6 ++ 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/modules/programs/nh.nix b/modules/programs/nh.nix index 0f0283f0..5b334170 100644 --- a/modules/programs/nh.nix +++ b/modules/programs/nh.nix @@ -29,6 +29,42 @@ in ''; }; + osFlake = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + The string that will be used for the {env}`NH_OS_FLAKE` environment variable. + + {env}`NH_OS_FLAKE` is used by nh as the default flake for performing {command}`nh os` + actions, such as {command}`nh os switch`. + Setting this will take priority over the `flake` option. + ''; + }; + + homeFlake = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + The string that will be used for the {env}`NH_HOME_FLAKE` environment variable. + + {env}`NH_HOME_FLAKE` is used by nh as the default flake for performing {command}`nh home` + actions, such as {command}`nh home switch`. + Setting this will take priority over the `flake` option. + ''; + }; + + darwinFlake = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + description = '' + The string that will be used for the {env}`NH_DARWIN_FLAKE` environment variable. + + {env}`NH_DARWIN_FLAKE` is used by nh as the default flake for performing + {command}`nh darwin` actions, such as {command}`nh darwin switch`. + Setting this will take priority over the `flake` option. + ''; + }; + clean = { enable = lib.mkEnableOption '' periodic garbage collection for user profile and nix store with nh clean @@ -64,19 +100,48 @@ in lib.optional (cfg.clean.enable && config.nix.gc.automatic) "programs.nh.clean.enable and nix.gc.automatic (Home-Manager) are both enabled. Please use one or the other to avoid conflict."; - assertions = lib.optionals pkgs.stdenv.isDarwin [ - (lib.hm.darwin.assertInterval "programs.nh.clean.dates" cfg.clean.dates pkgs) - ]; + assertions = + (lib.optionals pkgs.stdenv.isDarwin [ + (lib.hm.darwin.assertInterval "programs.nh.clean.dates" cfg.clean.dates pkgs) + ]) + ++ + map + (name: { + assertion = (cfg.${name} != null) -> !(lib.hasSuffix ".nix" cfg.${name}); + message = "nh.${name} must be a directory, not a nix file"; + }) + [ + "darwinFlake" + "flake" + "homeFlake" + "osFlake" + ]; home = lib.mkIf cfg.enable { packages = [ cfg.package ]; - sessionVariables = lib.mkIf (cfg.flake != null) ( - let - packageVersion = lib.getVersion cfg.package; - isVersion4OrHigher = lib.versionAtLeast packageVersion "4.0.0"; - in - if isVersion4OrHigher then { NH_FLAKE = cfg.flake; } else { FLAKE = cfg.flake; } - ); + sessionVariables = lib.mkMerge [ + (lib.mkIf (cfg.flake != null) ( + let + packageVersion = lib.getVersion cfg.package; + isVersion4OrHigher = lib.versionAtLeast packageVersion "4.0.0"; + in + if isVersion4OrHigher then { NH_FLAKE = cfg.flake; } else { FLAKE = cfg.flake; } + )) + (lib.mkMerge ( + map + ( + name: + (lib.mkIf (cfg."${name}Flake" != null) { + "NH_${lib.toUpper name}_FLAKE" = cfg."${name}Flake"; + }) + ) + [ + "darwin" + "home" + "os" + ] + )) + ]; }; systemd.user = lib.mkIf (cfg.clean.enable && pkgs.stdenv.isLinux) { diff --git a/tests/modules/programs/nh/darwin/config.nix b/tests/modules/programs/nh/darwin/config.nix index 20f9c821..64be9781 100644 --- a/tests/modules/programs/nh/darwin/config.nix +++ b/tests/modules/programs/nh/darwin/config.nix @@ -3,6 +3,9 @@ enable = true; flake = "/path/to/flake"; + osFlake = "/path/to/osFlake"; + homeFlake = "/path/to/homeFlake"; + darwinFlake = "/path/to/darwinFlake"; clean = { enable = true; @@ -18,5 +21,8 @@ assertFileContent $serviceFileNormalized ${./launchd.plist} assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_FLAKE="/path/to/flake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_OS_FLAKE="/path/to/osFlake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_HOME_FLAKE="/path/to/homeFlake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_DARWIN_FLAKE="/path/to/darwinFlake"' ''; } diff --git a/tests/modules/programs/nh/linux/config.nix b/tests/modules/programs/nh/linux/config.nix index 5cdf78d9..9797b6c6 100644 --- a/tests/modules/programs/nh/linux/config.nix +++ b/tests/modules/programs/nh/linux/config.nix @@ -5,6 +5,9 @@ package = config.lib.test.mkStubPackage { version = "4.0.0"; }; flake = "/path/to/flake"; + osFlake = "/path/to/osFlake"; + homeFlake = "/path/to/homeFlake"; + darwinFlake = "/path/to/darwinFlake"; clean = { enable = true; @@ -31,5 +34,8 @@ assertFileExists $unitDir/timers.target.wants/nh-clean.timer assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_FLAKE="/path/to/flake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_OS_FLAKE="/path/to/osFlake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_HOME_FLAKE="/path/to/homeFlake"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_DARWIN_FLAKE="/path/to/darwinFlake"' ''; }