fork/home-manager

git clone https://git.julia.blue/fork/home-manager

TNEnixos: only refer to `users.users` if neededca4126e3

master
3.5 KiB116 linesraw
1# This module is the common base for the NixOS and nix-darwin modules.
2# For OS-specific configuration, please edit nixos/default.nix or nix-darwin/default.nix instead.
3
4{ config, lib, pkgs, ... }:
5
6with lib;
7
8let
9
10  cfg = config.home-manager;
11
12  extendedLib = import ../modules/lib/stdlib-extended.nix lib;
13
14  hmModule = types.submoduleWith {
15    description = "Home Manager module";
16    specialArgs = {
17      lib = extendedLib;
18      osConfig = config;
19      modulesPath = builtins.toString ../modules;
20    } // cfg.extraSpecialArgs;
21    modules = [
22      ({ name, ... }: {
23        imports = import ../modules/modules.nix {
24          inherit pkgs;
25          lib = extendedLib;
26          useNixpkgsModule = !cfg.useGlobalPkgs;
27        };
28
29        config = {
30          submoduleSupport.enable = true;
31          submoduleSupport.externalPackageInstall = cfg.useUserPackages;
32
33          home.username = config.users.users.${name}.name;
34          home.homeDirectory = config.users.users.${name}.home;
35
36          # Make activation script use same version of Nix as system as a whole.
37          # This avoids problems with Nix not being in PATH.
38          nix.package = config.nix.package;
39        };
40      })
41    ] ++ cfg.sharedModules;
42  };
43
44in {
45  options.home-manager = {
46    useUserPackages = mkEnableOption ''
47      installation of user packages through the
48      {option}`users.users.<name>.packages` option'';
49
50    useGlobalPkgs = mkEnableOption ''
51      using the system configuration's `pkgs`
52      argument in Home Manager. This disables the Home Manager
53      options {option}`nixpkgs.*`'';
54
55    backupFileExtension = mkOption {
56      type = types.nullOr types.str;
57      default = null;
58      example = "backup";
59      description = ''
60        On activation move existing files by appending the given
61        file extension rather than exiting with an error.
62      '';
63    };
64
65    extraSpecialArgs = mkOption {
66      type = types.attrs;
67      default = { };
68      example = literalExpression "{ inherit emacs-overlay; }";
69      description = ''
70        Extra `specialArgs` passed to Home Manager. This
71        option can be used to pass additional arguments to all modules.
72      '';
73    };
74
75    sharedModules = mkOption {
76      type = with types; listOf raw;
77      default = [ ];
78      example = literalExpression "[ { home.packages = [ nixpkgs-fmt ]; } ]";
79      description = ''
80        Extra modules added to all users.
81      '';
82    };
83
84    verbose = mkEnableOption "verbose output on activation";
85
86    users = mkOption {
87      type = types.attrsOf hmModule;
88      default = { };
89      # Prevent the entire submodule being included in the documentation.
90      visible = "shallow";
91      description = ''
92        Per-user Home Manager configuration.
93      '';
94    };
95  };
96
97  config = (mkMerge [
98    # Fix potential recursion when configuring home-manager users based on values in users.users #594
99    (mkIf (cfg.useUserPackages && cfg.users != { }) {
100      users.users =
101        (mapAttrs (username: usercfg: { packages = [ usercfg.home.path ]; })
102          cfg.users);
103      environment.pathsToLink = [ "/etc/profile.d" ];
104    })
105    (mkIf (cfg.users != { }) {
106      warnings = flatten (flip mapAttrsToList cfg.users (user: config:
107        flip map config.warnings (warning: "${user} profile: ${warning}")));
108
109      assertions = flatten (flip mapAttrsToList cfg.users (user: config:
110        flip map config.assertions (assertion: {
111          inherit (assertion) assertion;
112          message = "${user} profile: ${assertion.message}";
113        })));
114    })
115  ]);
116}