old
1{
2 den,
3 inputs,
4 ...
5}: {
6 flake-file.inputs.impermanence = {
7 url = "github:nix-community/impermanence";
8 inputs.nixpkgs.follows = "nixpkgs";
9 inputs.home-manager.follows = "home-manager";
10 };
11
12 den.aspects.core.impermanence = {
13 includes = with den.aspects.core.impermanence; [
14 persist-collector
15 persist-user-collector
16 persist-home-collector
17
18 btrfs
19 ];
20
21 persist = {
22 directories = [
23 "/etc/NetworkManager/system-connections"
24 ];
25 files = [
26 "/root/.bash_history"
27 ];
28 };
29 cache = {
30 directories = [
31 "/var/lib/nixos"
32 "/srv"
33 ];
34 };
35 persistUser = {
36 files = [
37 ".bash_history"
38 ];
39 };
40
41 nixos = {
42 pkgs,
43 lib,
44 config,
45 ...
46 }: {
47 imports = [inputs.impermanence.nixosModules.impermanence];
48
49 options = {
50 users.users = lib.mkOption {
51 type = lib.types.attrsOf (lib.types.submodule ({name, ...}: {
52 options = {
53 persistence = let
54 userName = name;
55 outConfig = config;
56 in
57 lib.mkOption {
58 type = lib.types.attrsOf (lib.types.submodule ({
59 name,
60 config,
61 ...
62 }:
63 lib.modules.importApply "${inputs.impermanence}/submodule-options.nix" {
64 inherit pkgs lib name;
65 config = outConfig.environment.persistence.${name} // config;
66 usersOpts = true;
67 user = userName;
68 group = outConfig.users.users.${userName}.group;
69 homeDir = outConfig.users.users.${userName}.home;
70 }));
71 default = {};
72 };
73 };
74 }));
75 };
76 };
77
78 config = let
79 filterRemovedOptions = attrs:
80 (removeAttrs attrs ["home"])
81 // {directories = map (dir: removeAttrs dir ["method"]) attrs.directories;};
82 in {
83 environment.persistence.persist = {
84 enable = true;
85 persistentStoragePath = "/persist";
86 hideMounts = lib.mkDefault true;
87 users =
88 lib.mapAttrs (_: u: filterRemovedOptions (u.persistence.persist or {}))
89 (lib.filterAttrs (_: u: u.persistence ? persist) config.users.users);
90 };
91 environment.persistence.cache = {
92 enable = true;
93 persistentStoragePath = "/cache";
94 hideMounts = lib.mkDefault true;
95 users =
96 lib.mapAttrs (_: u: filterRemovedOptions (u.persistence.cache or {}))
97 (lib.filterAttrs (_: u: u.persistence ? cache) config.users.users);
98 };
99 };
100 };
101 };
102}