summaryrefslogtreecommitdiffstats
path: root/modules/finix/base.nix
blob: e549ca28653a66d5ec6f8ca38be3f5dd37931183 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
  config,
  hjem-lib,
  lib,
  options,
  pkgs,
  utils,
  ...
}: let
  inherit
    (builtins)
    attrNames
    attrValues
    concatLists
    concatMap
    concatStringsSep
    filter
    listToAttrs
    toJSON
    typeOf
    ;
  inherit (hjem-lib) fileToJson;
  inherit (lib.attrsets) filterAttrs nameValuePair optionalAttrs;
  inherit (lib.meta) getExe;
  inherit
    (lib.modules)
    importApply
    mkDefault
    mkMerge
    ;
  inherit (lib.strings) concatMapStringsSep;
  inherit (lib.trivial) flip pipe;
  inherit (lib.types) submoduleWith;

  osConfig = config;

  cfg = config.hjem;
  _class = "nixos";

  enabledUsers = filterAttrs (_: u: u.enable) cfg.users;
  disabledUsers = filterAttrs (_: u: !u.enable) cfg.users;

  userFiles = user: [
    user.files
    user.xdg.cache.files
    user.xdg.config.files
    user.xdg.data.files
    user.xdg.state.files
  ];

  hjemCli = getExe cfg.cli.package;
  hjemPkg = cfg.cli.package;
  actualLinker =
    if cfg.linker == null
    then hjemPkg
    else cfg.linker;
  useExternalLinker = actualLinker != hjemPkg;
  linkerExe = getExe actualLinker;
  prefix =
    if (typeOf cfg.linkerOptions == "set") && cfg.linkerOptions ? prefix
    then cfg.linkerOptions.prefix
    else ".backup-";
  linkerArgFlags =
    if !useExternalLinker
    then ""
    else if typeOf cfg.linkerOptions == "set"
    then let
      optsFile = pkgs.writeText "hjem-linker-options.json" (toJSON cfg.linkerOptions);
    in ''--linker-arg --linker-opts --linker-arg ${optsFile}''
    else concatMapStringsSep " " (arg: ''--linker-arg "${arg}"'') cfg.linkerOptions;
  externalLinkerFlags =
    if useExternalLinker
    then ''--external-linker "${linkerExe}" ${linkerArgFlags}''
    else "";

  newManifests = let
    writeManifest = user: let
      name = "manifest-${user.user}.json";
    in
      pkgs.writeTextFile {
        inherit name;
        destination = "/${name}";
        text = toJSON {
          version = 3;
          files = concatMap (flip pipe [
            attrValues
            (filter (x: x.enable))
            (map fileToJson)
          ]) (userFiles user);
        };
        checkPhase = ''
          set -e
          CUE_CACHE_DIR=$(pwd)/.cache
          CUE_CONFIG_DIR=$(pwd)/.config

          ${getExe pkgs.cue} vet -c ${../../manifest/v3.cue} $target
        '';
      };
  in
    pkgs.symlinkJoin {
      name = "hjem-manifests";
      paths = map writeManifest (attrValues enabledUsers);
    };

  hjemSubmodule = submoduleWith {
    description = "Hjem submodule for Finix";
    class = "hjem";
    specialArgs =
      cfg.specialArgs
      // {
        inherit
          hjem-lib
          osConfig
          pkgs
          utils
          ;
        osOptions = options;
      };
    modules = concatLists [
      [
        ../common/user.nix
        (
          {
            config,
            name,
            ...
          }: let
            user = osConfig.users.users.${name};
          in {
            assertions = [
              {
                assertion = config.enable -> user.enable;
                message = "Enabled Hjem user '${name}' must also be configured and enabled in NixOS.";
              }
            ];

            user = mkDefault user.name;
            directory = mkDefault user.home;
            clobberFiles = mkDefault cfg.clobberByDefault;
          }
        )
      ]
      # Evaluate additional modules under 'hjem.users.<username>' so that
      # module systems built on Hjem are more ergonomic.
      cfg.extraModules
    ];
  };
in {
  inherit _class;

  imports = [
    (importApply ../common/top-level.nix {inherit hjemSubmodule _class;})
  ];

  config = mkMerge [
    (optionalAttrs (options ? system.extraDependencies) {
      system.extraDependencies = concatMap (u: u.extraDependencies) (attrValues enabledUsers);
    })

    {
      finit.tasks = let
        oldManifests = "/var/lib/hjem";
      in
        {
          hjem-prepare = {
            description = "Prepare Hjem manifests directory";
            command = pkgs.writeShellScript "hjem-prepare" ''
              mkdir -p ${oldManifests}
            '';
          };

          hjem-cleanup = {
            description = "Cleanup disabled users' manifests";
            conditions = map (user: "task/hjem-copy-${user.user}/success") (attrValues enabledUsers);
            command = pkgs.writeShellScript "hjem-cleanup" (
              if disabledUsers != {}
              then "rm -f ${
                concatStringsSep " " (map (user: "${oldManifests}/manifest-${user.user}.json") (attrValues disabledUsers))
              }"
              else "true"
            );
          };
        }
        // optionalAttrs (enabledUsers != {}) (
          listToAttrs (
            concatMap (
              user: let
                username = user.user;
                activateName = "hjem-activate-${username}";
                copyName = "hjem-copy-${username}";
              in [
                (nameValuePair activateName {
                  description = "Link files for ${username} from their manifest";
                  user = username;
                  conditions = ["task/hjem-prepare/success"];
                  command = pkgs.writeShellScript activateName ''
                    new_manifest="${newManifests}/manifest-${username}.json"
                    old_manifest="${oldManifests}/manifest-${username}.json"

                    ${hjemCli} internal activate \
                      --manifest "$new_manifest" \
                      --state "$old_manifest" \
                      --skip-state-update \
                      --prefix "${prefix}" \
                      ${externalLinkerFlags} \
                      --json
                  '';
                })
                (nameValuePair copyName {
                  description = "Update Hjem state manifest for ${username}";
                  conditions = ["task/${activateName}/success"];
                  command = pkgs.writeShellScript copyName ''
                    ${hjemCli} internal update-state \
                      --manifest "${newManifests}/manifest-${username}.json" \
                      --state "${oldManifests}/manifest-${username}.json"
                  '';
                })
              ]
            ) (attrValues enabledUsers)
          )
        );
    }
  ];
}