about summary refs log tree commit diff
path: root/modules/home/default.nix
blob: 0ad1cb85b255f64954e7c48aea019039cf289dda (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
{ lib, config, ... }:

let
  inherit (lib) attrValues concatStringsSep mapAttrsToList mkOption
                optionalString recursiveUpdate;
  inherit (lib.types) bool loaOf nullOr str submodule;

  dirOpts = { ... }: {
    options = {
      owner = mkOption {
        default = null;
        type = nullOr str;
      };
      group = mkOption {
        default = "users";
        type = str;
      };
      permissions = mkOption {
        default = "0700";
        type = str;
      };
      activationScripts = mkOption {
        default = {};
        type = loaOf str;
      };
    };
  };

  applyDirConfig = user: dir:
    let
      owner = if dir.owner == null
              then user
              else config.users.users.${dir.owner};
    in
      ''
        chmod ${dir.permissions} .
        chown ${owner.name}:${dir.group} .
        ${concatStringsSep "\n" (attrValues dir.activationScripts)}
      '';

in
  {
    options = {
      home = mkOption {
        default = {};
        type = loaOf (submodule (args: recursiveUpdate (dirOpts args) {
          options = {
            imperativeNix = mkOption {
              default = false;
              type = bool;
            };
            dirs = mkOption {
              default = {};
              type = loaOf (submodule dirOpts);
            };
          };
        }));
      };
    };

    config = {
      system.activationScripts.home = {
        deps = [];
        text = concatStringsSep "\n" (mapAttrsToList
          (key: home:
            let
              user = config.users.users.${key};

            in ''
              ${optionalString (!home.imperativeNix) ''
                rm -rf ${user.home}/.nix-{defexpr,profile}
              ''}

              pushd ${user.home} >/dev/null
              ${applyDirConfig user home}
              ${concatStringsSep "\n" (mapAttrsToList (name: dir: ''
                mkdir -p ${name}
                pushd ${name} >/dev/null
                ${applyDirConfig user dir}
                popd >/dev/null
              '') home.dirs)}
              popd >/dev/null
            ''
        ) config.home);
      };
    };
  }