aboutsummaryrefslogtreecommitdiff
path: root/lib/default.nix
blob: 542480111420f0194ca4ab9b3a20d70bc0e773be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{ inputs, ... }:

let
  lib = inputs.nixpkgs.lib;
  isDarwin = s: lib.strings.hasSuffix "-darwin" s;
in
{
  mkUser =
    {
      username,
      homeDirectory ? null,
      useHomeManager ? true,
      extraHomeModules ? [ ],
    }:
    {
      config,
      pkgs,
      lib,
      system,
      inputs,
      ...
    }:
    let
      inherit (lib)
        mkOption
        types
        mkIf
        mkMerge
        optional
        optionals
        ;

      darwinHost = isDarwin system;
      defaultHome = if darwinHost then "/Users/${username}" else "/home/${username}";
      home = if homeDirectory != null then homeDirectory else defaultHome;

      anyDesktopEnabled =
        (config.nx.desktop.kde.enable or false)
        || (config.nx.desktop.hyprland.enable or false)
        || (config.nx.desktop.gnome.enable or false)
        || (config.nx.desktop.cinnamon.enable or false)
        || (config.nx.desktop.sway.enable or false)
        || (config.nx.desktop.labwc.enable or false);

      cfg = config.nx.user.${username};

      shellPkg = if (cfg.nx.terminal.defaultShell or "bash") == "zsh" then pkgs.zsh else pkgs.bash;
    in
    {
      options.nx.user.${username} = mkOption {
        description = "User configuration for ${username}";
        type = types.submodule {
          freeformType = types.attrsOf types.anything;
          options = {
            stateVersion = mkOption {
              type = types.str;
              description = "home.stateVersion for this user";
            };

            packages = mkOption {
              type = types.listOf types.package;
              default = [ ];
              description = "Extra packages for this user";
            };

            shellAliases = mkOption {
              type = types.attrsOf types.str;
              default = { };
              description = "Shell aliases";
            };

            sessionVariables = mkOption {
              type = types.attrsOf types.str;
              default = { };
              description = "Session environment variables";
            };

            nx = mkOption {
              type = types.attrsOf types.anything;
              default = { };
              description = "User module options (proxied to home-manager's nx.*)";
            };
          };
        };
        default = { };
      };

      config = mkMerge [
        (mkIf (!darwinHost) {
          users.users.${username} = {
            isNormalUser = true;
            home = home;
            shell = shellPkg;
            ignoreShellProgramCheck = true;
            extraGroups =
              [
                "wheel"
              ]
              ++ optional (config.networking.networkmanager.enable or false) "networkmanager"
              ++ optionals anyDesktopEnabled [
                "video"
                "input"
              ]
              ++ optional (config.nx.services.audio.enable or false) "audio"
              ++ optional (config.nx.printer.enable or false) "lp";
          };
        })

        (mkIf darwinHost {
          users.users.${username}.home = home;
        })

        (mkIf useHomeManager {
          home-manager.extraSpecialArgs = { inherit inputs; };

          home-manager.users.${username} = {
            home.username = username;
            home.homeDirectory = home;
            home.stateVersion = cfg.stateVersion;
            home.packages = cfg.packages;
            home.sessionVariables = cfg.sessionVariables;

            programs.home-manager.enable = true;

            programs.zsh.shellAliases = mkIf (cfg.nx.terminal.defaultShell == "zsh") cfg.shellAliases;
            programs.bash.shellAliases = mkIf (cfg.nx.terminal.defaultShell == "bash") cfg.shellAliases;

            imports = [ ../modules/users ] ++ extraHomeModules;

            nx = cfg.nx;
          };
        })
      ];
    };

  mkSystem =
    {
      host,
      username,
      system,
      overlays ? [ ],
      extraModules ? [ ],
      extraSpecialArgs ? { },
      useHomeManager ? true,
      extraHomeModules ? [ ],
    }:
    let
      darwinHost = isDarwin system;
      builder =
        if darwinHost then inputs.nix-darwin.lib.darwinSystem else inputs.nixpkgs.lib.nixosSystem;
      hostDir = ../hosts/${host};
      hostCfg = hostDir + /configuration.nix;

      self = import ./. { inherit inputs; };

      nixpkgsModule = {
        nixpkgs.overlays = overlays;
        nixpkgs.config.allowUnfree = true;

        nix.settings.experimental-features = [
          "nix-command"
          "flakes"
        ];
      };

      modules =
        [
          hostCfg
          nixpkgsModule
        ]
        ++ (lib.optional (!darwinHost) ../modules/hosts)
        ++ (lib.optional useHomeManager (
          if darwinHost then
            inputs.home-manager.darwinModules.home-manager
          else
            inputs.home-manager.nixosModules.home-manager
        ))
        ++ (lib.optional useHomeManager (self.mkUser {
          inherit
            username
            useHomeManager
            extraHomeModules
            ;
        }))
        ++ extraModules;
    in
    builder {
      system = system;
      specialArgs = (
        {
          inherit
            inputs
            system
            host
            username
            useHomeManager
            ;
        }
        // extraSpecialArgs
      );
      modules = modules;
    };
}