summaryrefslogtreecommitdiffstats
path: root/modules/options.nix
blob: e4291711273c531d22e545a8d33f3d03c1505fe0 (plain) (blame)
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
# Defines the user and home manager options (credit to hlissner, KubqoA)
{
  config,
  options,
  lib,
  home-manager,
  ...
}: {
  options = {
    user = lib.mkOption {
      type = lib.types.attrs;
      default = {};
    };
    nixosConfig = {
      dir = lib.mkOption {
        type = lib.types.path;
        default = lib.removePrefix "/mnt" (
          lib.findFirst lib.pathExists (toString ../.) [
            "/mnt/etc/nixos"
            "/mnt/nixos"
          ]
        );
      };
      binDir = lib.mkOption {
        type = lib.types.path;
        default = "${config.nixosConfig.dir}/bin";
      };
      configDir = lib.mkOption {
        type = lib.types.path;
        default = "${config.nixosConfig.dir}/config";
      };
      modulesDir = lib.mkOption {
        type = lib.types.path;
        default = "${config.nixosConfig.dir}/modules";
      };
    };
    home = {
      activation = lib.mkOption {
        type = lib.types.attrs;
        default = {};
      };
      file = lib.mkOption {
        type = lib.types.attrs;
        default = {};
      };
      configFile = lib.mkOption {
        type = lib.types.attrs;
        default = {};
      };
      manager = lib.mkOption {
        type = lib.types.attrs;
        default = {};
      };
      packages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [];
      };
    };
    env = lib.mkOption {
      type = lib.types.attrsOf (
        lib.types.oneOf [
          lib.types.str
          lib.types.path
          (
            lib.types.listOf (lib.types.either lib.types.str lib.types.path)
          )
        ]
      );
      apply = lib.mapAttrs (n: v:
        if lib.isList v
        then lib.concatMapStringsSep ":" (x: toString x) v
        else (toString v));
      default = {};
    };
  };

  config = {
    user = let
      defaultUser = "jon";
      user = builtins.getEnv "USER";
      name =
        if lib.elem user ["" "root"]
        then defaultUser
        else user;
    in {
      inherit name;
      description = "Primary user account";
      home = "/home/${name}";
      isNormalUser = true;
      group = "users";
      extraGroups = ["wheel"];
      uid = 1000;
    };

    # home manager configuration
    home-manager = {
      useUserPackages = true;
      users.${config.user.name} = lib.mkAliasDefinitions options.home.manager;
    };

    # home.manager is an alias for having to write home-manager.users.<user>
    home.manager = {
      home = {
        homeDirectory = config.user.home;
        stateVersion = config.system.stateVersion;
        activation = lib.mkAliasDefinitions options.home.activation;
        file = lib.mkAliasDefinitions options.home.file;
        packages = lib.mkAliasDefinitions options.home.packages;
      };
      xdg = {
        enable = true;
        configFile = lib.mkAliasDefinitions options.home.configFile;
      };
    };

    # link to user config
    users.users.${config.user.name} = lib.mkAliasDefinitions options.user;

    # nix configuration set new user and root as trusted to connect to the nix daemon
    nix.settings = let
      users = ["root" config.user.name];
    in {
      trusted-users = users;
      allowed-users = users;
    };

    # set default XDG dirs
    environment = {
      sessionVariables = {
        XDG_CACHE_HOME = "$HOME/.cache";
        XDG_CONFIG_HOME = "$HOME/.config";
        XDG_DATA_HOME = "$HOME/.local/share";
        XDG_BIN_HOME = "$HOME/.local/bin/";
      };
      extraInit =
        lib.concatStringsSep "\n"
        (lib.mapAttrsToList (n: v: ''export ${n}="${v}"'') config.env);
    };

    # add user binary path to PATH
    env.PATH = ["$NIXOSCONFIG_BIN" "$XDG_BIN_HOME" "$PATH"];
  };
}