1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-26 21:19:45 +01:00
home-manager/nixos/default.nix
Michael Hoang 192f123e4b
nixos: add key to shared module to allow disabling it (#6017)
By adding `key`, this allows users to disable this shared module or they can choose to not disable this shared module (by filtering by key before disabling)

This means users can disable all shared modules if all modules are paths or attrsets with a key:

`configuration.nix`:

```nix
{ config, ... }:

{
  home-manager.users.enzime = { ... }: {
    disabledModules = config.home-manager.sharedModules;
  };
}
```

Or disabling just this module specifically:

```nix
{ ... }:

{
  home-manager.users.enzime = { ... }: {
    disabledModules = [ { key = "home-manager#nixos-shared-module"; } ];
  };
}
```

Or disabling all modules when you have modules you can't disable (like lambdas):

```nix
{ ... }:

{
  home-manager.users.enzime = { ... }: {
    disabledModules = lib.filter (v: lib.isString v || lib.isPath v || (lib.isAttrs v && v ? key)) config.home-manager.sharedModules;
  };
}
```


https://nixos.org/manual/nixos/unstable/#sec-replace-modules
2024-11-16 18:31:16 +01:00

91 lines
2.8 KiB
Nix

{ config, lib, pkgs, utils, ... }:
with lib;
let
cfg = config.home-manager;
serviceEnvironment = optionalAttrs (cfg.backupFileExtension != null) {
HOME_MANAGER_BACKUP_EXT = cfg.backupFileExtension;
} // optionalAttrs cfg.verbose { VERBOSE = "1"; };
in {
imports = [ ./common.nix ];
config = mkMerge [
{
home-manager = {
extraSpecialArgs.nixosConfig = config;
sharedModules = [{
key = "home-manager#nixos-shared-module";
config = {
# The per-user directory inside /etc/profiles is not known by
# fontconfig by default.
fonts.fontconfig.enable = lib.mkDefault
(cfg.useUserPackages && config.fonts.fontconfig.enable);
# Inherit glibcLocales setting from NixOS.
i18n.glibcLocales = lib.mkDefault config.i18n.glibcLocales;
};
}];
};
}
(mkIf (cfg.users != { }) {
systemd.services = mapAttrs' (_: usercfg:
let username = usercfg.home.username;
in nameValuePair ("home-manager-${utils.escapeSystemdPath username}") {
description = "Home Manager environment for ${username}";
wantedBy = [ "multi-user.target" ];
wants = [ "nix-daemon.socket" ];
after = [ "nix-daemon.socket" ];
before = [ "systemd-user-sessions.service" ];
environment = serviceEnvironment;
unitConfig = { RequiresMountsFor = usercfg.home.homeDirectory; };
stopIfChanged = false;
serviceConfig = {
User = usercfg.home.username;
Type = "oneshot";
TimeoutStartSec = "5m";
SyslogIdentifier = "hm-activate-${username}";
ExecStart = let
systemctl =
"XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/run/user/$UID} systemctl";
sed = "${pkgs.gnused}/bin/sed";
exportedSystemdVariables = concatStringsSep "|" [
"DBUS_SESSION_BUS_ADDRESS"
"DISPLAY"
"WAYLAND_DISPLAY"
"XAUTHORITY"
"XDG_RUNTIME_DIR"
];
setupEnv = pkgs.writeScript "hm-setup-env" ''
#! ${pkgs.runtimeShell} -el
# The activation script is run by a login shell to make sure
# that the user is given a sane environment.
# If the user is logged in, import variables from their current
# session environment.
eval "$(
${systemctl} --user show-environment 2> /dev/null \
| ${sed} -En '/^(${exportedSystemdVariables})=/s/^/export /p'
)"
exec "$1/activate"
'';
in "${setupEnv} ${usercfg.home.activationPackage}";
};
}) cfg.users;
})
];
}