diff --git a/nixos/common.nix b/nixos/common.nix index ee9a28cc9..b47b5c8e0 100644 --- a/nixos/common.nix +++ b/nixos/common.nix @@ -53,9 +53,16 @@ let }; home = { + uid = + let + # `users.users..uid` may be declared but unset on + # nix-darwin, so probe it with `tryEval` instead of forcing a + # no-value-defined error during module evaluation. + userUid = builtins.tryEval config.users.users.${name}.uid; + in + mkIf (userUid.success && userUid.value != null) userUid.value; username = config.users.users.${name}.name; homeDirectory = config.users.users.${name}.home; - uid = mkIf (options.users.users.${name}.uid.isDefined or false) config.users.users.${name}.uid; }; nix = { diff --git a/tests/modules/home-environment/default.nix b/tests/modules/home-environment/default.nix index c8dc446b8..6d0856d07 100644 --- a/tests/modules/home-environment/default.nix +++ b/tests/modules/home-environment/default.nix @@ -3,6 +3,7 @@ home-session-search-variables = ./session-search-variables.nix; home-session-variables = ./session-variables.nix; home-nixpkgs-release-check-pkgs = ./nixpkgs-release-check-pkgs.nix; + home-uid-from-nixos = ./uid-from-nixos.nix; home-uid = ./uid.nix; home-uid-null = ./uid-null.nix; } diff --git a/tests/modules/home-environment/uid-from-nixos.nix b/tests/modules/home-environment/uid-from-nixos.nix new file mode 100644 index 000000000..43687e90c --- /dev/null +++ b/tests/modules/home-environment/uid-from-nixos.nix @@ -0,0 +1,115 @@ +{ lib, pkgs, ... }: +let + nixosLib = import /${pkgs.path}/nixos/lib { inherit lib; }; + + getHomeUid = + userConfig: + (nixosLib.evalTest { + hostPkgs = pkgs; + nodes.machine.imports = [ + ../../../nixos + { + users.users.alice = { + isNormalUser = true; + } + // userConfig; + + home-manager.users.alice.home.stateVersion = "24.11"; + } + ]; + }).config.nodes.machine.home-manager.users.alice.home.uid; + + getCommonModuleHomeUidResult = + userConfig: + builtins.tryEval ( + (lib.evalModules { + specialArgs = { + inherit pkgs; + _class = "darwin"; + }; + + modules = [ + ../../../nixos/common.nix + (_: { + options.users.users = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule ( + { name, ... }: + { + options = { + name = lib.mkOption { + type = lib.types.str; + default = name; + }; + home = lib.mkOption { type = lib.types.str; }; + uid = lib.mkOption { type = lib.types.int; }; + packages = lib.mkOption { + type = lib.types.listOf lib.types.package; + default = [ ]; + }; + }; + } + ) + ); + default = { }; + }; + + options.environment.pathsToLink = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + }; + + options.nix.enable = lib.mkOption { + type = lib.types.bool; + default = true; + }; + + options.nix.package = lib.mkOption { + type = lib.types.package; + default = pkgs.nix; + }; + + options.warnings = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = [ ]; + }; + + options.assertions = lib.mkOption { + type = lib.types.listOf ( + lib.types.submodule (_: { + options.assertion = lib.mkOption { type = lib.types.bool; }; + options.message = lib.mkOption { type = lib.types.str; }; + }) + ); + default = [ ]; + }; + + config.users.users.alice = { + home = "/Users/alice"; + } + // userConfig; + + config.home-manager.users.alice.home.stateVersion = "24.11"; + }) + ]; + }).config.home-manager.users.alice.home.uid + ); + + forwardedUid = builtins.toJSON (getHomeUid { + uid = 1000; + }); + unsetUid = builtins.toJSON (getHomeUid { }); + commonUnsetUidResult = getCommonModuleHomeUidResult { }; + commonUnsetUidSuccess = builtins.toJSON commonUnsetUidResult.success; + commonUnsetUidValue = builtins.toJSON ( + if commonUnsetUidResult.success then commonUnsetUidResult.value else "failed" + ); +in +{ + nmt.script = '' + test "${forwardedUid}" = '1000' + test "${unsetUid}" = 'null' + test "${commonUnsetUidSuccess}" = 'true' + test "${commonUnsetUidValue}" = 'null' + ''; +}