diff --git a/flake.lock b/flake.lock index 4739f01..350e8e5 100644 --- a/flake.lock +++ b/flake.lock @@ -269,16 +269,15 @@ ] }, "locked": { - "lastModified": 1738780311, - "narHash": "sha256-78uYeNb6yGvd2PClTNbtY25AdLyND1Sv6oMN1reN2ww=", - "owner": "nezia1", + "lastModified": 1737619027, + "narHash": "sha256-jEzZs9dHdmVP5X9HCC/7jrv08aWFfqZV5cZ+cZWYGA4=", + "owner": "feel-co", "repo": "hjem", - "rev": "f8b3fb8f46aba8768001cca1936ca3d40d7e12ab", + "rev": "48cfa21987672a31a358b7e4d582fc174556e633", "type": "github" }, "original": { - "owner": "nezia1", - "ref": "implement-environment-variables", + "owner": "feel-co", "repo": "hjem", "type": "github" } diff --git a/flake.nix b/flake.nix index fa26c9f..563d992 100644 --- a/flake.nix +++ b/flake.nix @@ -113,7 +113,7 @@ inputs.nixpkgs.follows = "nixpkgs"; }; hjem = { - url = "github:nezia1/hjem/implement-environment-variables"; # TODO: change to github:feel-co/hjem if env var PR gets in + url = "github:feel-co/hjem"; inputs.nixpkgs.follows = "nixpkgs"; }; nix-gaming = { diff --git a/hosts/default.nix b/hosts/default.nix index ec69743..24ebd3b 100644 --- a/hosts/default.nix +++ b/hosts/default.nix @@ -5,10 +5,7 @@ specialArgs = {inherit inputs lib';}; modules = (args.modules or []) - ++ [ - ../modules - inputs.hjem.nixosModules.default - ]; + ++ [../modules]; }; in { vamos = mkSystem { diff --git a/modules/core/users.nix b/modules/core/users.nix index ed82d92..cd32129 100644 --- a/modules/core/users.nix +++ b/modules/core/users.nix @@ -1,13 +1,17 @@ { + inputs, lib, config, ... }: let - inherit (lib) mkIf; + inherit (lib.modules) mkIf; + inherit (lib.lists) singleton; + inherit (lib.filesystem) listFilesRecursive; inherit (config.local.systemVars) username; inherit (config.local.homeVars) fullName; inherit (config.local.profiles) desktop; in { + imports = [inputs.hjem.nixosModules.default]; users.users.${username} = { isNormalUser = true; description = fullName; @@ -26,9 +30,11 @@ in { enable = true; directory = "/home/${username}"; user = "${username}"; - environment = { - forceOverride = true; - }; + environment.forceOverride = true; + }; + + extraModules = singleton { + imports = listFilesRecursive ../../shared/modules/hjem; }; }; } diff --git a/shared/modules/hjem/environment.nix b/shared/modules/hjem/environment.nix new file mode 100644 index 0000000..6d01add --- /dev/null +++ b/shared/modules/hjem/environment.nix @@ -0,0 +1,74 @@ +{ + config, + lib, + ... +}: let + inherit (lib.options) mkOption; + inherit (lib.types) attrsOf listOf oneOf bool int path str; + inherit (lib.attrsets) attrNames mapAttrsToList; + inherit (lib.strings) concatStringsSep; + inherit (lib.lists) elem filter isList; + + envFile = "99-user-env.conf"; + cfg = config.environment; + + toEnv = env: + if isList env + then concatStringsSep ":" (map toString env) + else toString env; + + toConf = attrs: + concatStringsSep "\n" + (mapAttrsToList (name: value: "${name}=\"${toEnv value}\"") attrs); +in { + options.environment = { + sessionVariables = mkOption { + type = attrsOf (oneOf [(listOf (oneOf [int str path])) int str path]); + default = {}; + example = { + EDITOR = "nvim"; + VISUAL = "nvim"; + }; + description = '' + A set of environment variables used in the user environment. + These variables will be set as systemd user environment + variables, using `environment.d`. The value of each + variable can be either a string or a list of strings. The + latter is concatenated, interspersed with colon + characters. + ''; + }; + forceOverride = mkOption { + type = bool; + default = false; + example = true; + description = '' + Whether to override environment variables that might exist. + This is useful for variables such as {env}`EDITOR`, which are set by + default on NixOS. + ''; + }; + }; + + config = { + files.".config/environment.d/${envFile}".text = toConf cfg.sessionVariables; + + warnings = let + overlappingVars = filter (x: elem x (attrNames config.environment.sessionVariables)) (attrNames cfg.sessionVariables); + in + if + !cfg.forceOverride + && overlappingVars != [] + then + map (name: '' + The session variable '${name}' is defined in both + `hjem.users..environment.sessionVariables` and + `config.environment.variables`. This may lead to conflicts. + + If you want the one defined in hjem to take precedence, make sure you + set `forceOverride` to `true`. + '') + overlappingVars + else []; + }; +}