treewide: add environment module from hjem PR

This commit adds the module I made for hjem (https://github.com/feel-co/hjem/pull/16). Needs to be removed after it gets merged.
This commit is contained in:
Anthony Rodriguez 2025-02-09 15:31:32 +01:00
parent 3540b0af76
commit f6fedf748f
Signed by: nezia
SSH key fingerprint: SHA256:Ihfpl0rUpqDevYqnzSR34OYfVLbDNkBiUjs3CpX4ykA
5 changed files with 91 additions and 15 deletions

11
flake.lock generated
View file

@ -269,16 +269,15 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1738780311, "lastModified": 1737619027,
"narHash": "sha256-78uYeNb6yGvd2PClTNbtY25AdLyND1Sv6oMN1reN2ww=", "narHash": "sha256-jEzZs9dHdmVP5X9HCC/7jrv08aWFfqZV5cZ+cZWYGA4=",
"owner": "nezia1", "owner": "feel-co",
"repo": "hjem", "repo": "hjem",
"rev": "f8b3fb8f46aba8768001cca1936ca3d40d7e12ab", "rev": "48cfa21987672a31a358b7e4d582fc174556e633",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nezia1", "owner": "feel-co",
"ref": "implement-environment-variables",
"repo": "hjem", "repo": "hjem",
"type": "github" "type": "github"
} }

View file

@ -113,7 +113,7 @@
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
hjem = { 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"; inputs.nixpkgs.follows = "nixpkgs";
}; };
nix-gaming = { nix-gaming = {

View file

@ -5,10 +5,7 @@
specialArgs = {inherit inputs lib';}; specialArgs = {inherit inputs lib';};
modules = modules =
(args.modules or []) (args.modules or [])
++ [ ++ [../modules];
../modules
inputs.hjem.nixosModules.default
];
}; };
in { in {
vamos = mkSystem { vamos = mkSystem {

View file

@ -1,13 +1,17 @@
{ {
inputs,
lib, lib,
config, config,
... ...
}: let }: let
inherit (lib) mkIf; inherit (lib.modules) mkIf;
inherit (lib.lists) singleton;
inherit (lib.filesystem) listFilesRecursive;
inherit (config.local.systemVars) username; inherit (config.local.systemVars) username;
inherit (config.local.homeVars) fullName; inherit (config.local.homeVars) fullName;
inherit (config.local.profiles) desktop; inherit (config.local.profiles) desktop;
in { in {
imports = [inputs.hjem.nixosModules.default];
users.users.${username} = { users.users.${username} = {
isNormalUser = true; isNormalUser = true;
description = fullName; description = fullName;
@ -26,9 +30,11 @@ in {
enable = true; enable = true;
directory = "/home/${username}"; directory = "/home/${username}";
user = "${username}"; user = "${username}";
environment = { environment.forceOverride = true;
forceOverride = true; };
};
extraModules = singleton {
imports = listFilesRecursive ../../shared/modules/hjem;
}; };
}; };
} }

View file

@ -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.<name>.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 [];
};
}