summaryrefslogtreecommitdiffstats
path: root/tests/nixos/xdg-linker.nix
blob: 046db0a99c0505f0761e81a9c4627612277e0102 (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
{
  hjemModule,
  hjemTest,
  lib,
  formats,
  pkgs,
  writeText,
}: let
  userHome = "/home/alice";
in
  hjemTest {
    name = "hjem-xdg-linker";
    nodes = {
      node1 = let
        inherit (lib.modules) mkIf;
        inherit (lib.strings) optionalString;

        xdg = {
          clobber,
          altLocation,
        }: {
          cache = {
            directory = mkIf altLocation (userHome + "/customCacheDirectory");
            files = {
              "foo" = {
                text = "Hello ${optionalString clobber "new "}world!";
                inherit clobber;
              };
            };
          };
          config = {
            directory = mkIf altLocation (userHome + "/customConfigDirectory");
            files = {
              "bar.json" = {
                generator = lib.generators.toJSON {};
                value = {bar = "Hello ${optionalString clobber "new "}second world!";};
                inherit clobber;
              };
            };
          };
          data = {
            directory = mkIf altLocation (userHome + "/customDataDirectory");
            files = {
              "baz.toml" = {
                generator = (formats.toml {}).generate "baz.toml";
                value = {baz = "Hello ${optionalString clobber "new "}third world!";};
                inherit clobber;
              };
            };
          };
          state = {
            directory = mkIf altLocation (userHome + "/customStateDirectory");
            files = {
              "foo" = {
                source = writeText "file-bar" "Hello ${optionalString clobber "new "}fourth world!";
                inherit clobber;
              };
            };
          };
        };
      in {
        imports = [hjemModule];

        # ensure nixless deployments work
        nix.enable = false;

        users.groups.alice = {};
        users.users.alice = {
          isNormalUser = true;
          home = userHome;
          password = "";
        };

        hjem = {
          linker = pkgs.smfh;
          users = {
            alice = {
              enable = true;
            };
          };
        };

        specialisation = {
          defaultFilesGetLinked.configuration = {
            hjem.users.alice = {
              xdg = xdg {
                clobber = false;
                altLocation = false;
              };
            };
          };
          altFilesGetLinked.configuration = {
            hjem.users.alice = {
              files.".config/foo".text = "Hello world!";
              xdg = xdg {
                clobber = false;
                altLocation = true;
              };
            };
          };
          altFilesGetOverwritten.configuration = {
            hjem.users.alice = {
              files.".config/foo" = {
                text = "Hello new world!";
                clobber = true;
              };
              xdg = xdg {
                clobber = true;
                altLocation = true;
              };
            };
          };
        };
      };
    };

    testScript = {nodes, ...}: let
      baseSystem = nodes.node1.system.build.toplevel;
      specialisations = "${baseSystem}/specialisation";
    in
      # py
      ''
        node1.succeed("loginctl enable-linger alice")

        with subtest("Default file locations get liked"):
          node1.succeed("${specialisations}/defaultFilesGetLinked/bin/switch-to-configuration test")
          node1.succeed("test -L ${userHome}/.cache/foo")
          node1.succeed("grep \"Hello world!\" ~alice/.cache/foo")
          node1.succeed("test -L ${userHome}/.config/bar.json")
          node1.succeed("grep \"Hello second world!\" ~alice/.config/bar.json")
          node1.succeed("test -L ${userHome}/.local/share/baz.toml")
          node1.succeed("grep \"Hello third world!\" ~alice/.local/share/baz.toml")
          node1.succeed("test -L ${userHome}/.local/state/foo")
          node1.succeed("grep \"Hello fourth world!\" ~alice/.local/state/foo")

        with subtest("Alternate file locations get linked"):
          node1.succeed("${specialisations}/altFilesGetLinked/bin/switch-to-configuration test")
          node1.succeed("test -L ${userHome}/customCacheDirectory/foo")
          node1.succeed("grep \"Hello world!\" ~alice/customCacheDirectory/foo")
          node1.succeed("test -L ${userHome}/customConfigDirectory/bar.json")
          node1.succeed("grep \"Hello second world!\" ~alice/customConfigDirectory/bar.json")
          node1.succeed("test -L ${userHome}/customDataDirectory/baz.toml")
          node1.succeed("grep \"Hello third world!\" ~alice/customDataDirectory/baz.toml")
          node1.succeed("test -L ${userHome}/customStateDirectory/foo")
          node1.succeed("grep \"Hello fourth world!\" ~alice/customStateDirectory/foo")
          # Same name as config test file to verify proper merging
          node1.succeed("test -L ${userHome}/.config/foo")
          node1.succeed("grep \"Hello world!\" ~alice/.config/foo")

        with subtest("Alternate file locations get overwritten when changed"):
          node1.succeed("${specialisations}/altFilesGetLinked/bin/switch-to-configuration test")
          node1.succeed("${specialisations}/altFilesGetOverwritten/bin/switch-to-configuration test")
          node1.succeed("test -L ${userHome}/customCacheDirectory/foo")
          node1.succeed("grep \"Hello new world!\" ~alice/customCacheDirectory/foo")
          node1.succeed("test -L ${userHome}/customConfigDirectory/bar.json")
          node1.succeed("grep \"Hello new second world!\" ~alice/customConfigDirectory/bar.json")
          node1.succeed("test -L ${userHome}/customDataDirectory/baz.toml")
          node1.succeed("grep \"Hello new third world!\" ~alice/customDataDirectory/baz.toml")
          node1.succeed("test -L ${userHome}/customStateDirectory/foo")
          node1.succeed("grep \"Hello new fourth world!\" ~alice/customStateDirectory/foo")
          # Same name as config test file to verify proper merging
          node1.succeed("test -L ${userHome}/.config/foo")
          node1.succeed("grep \"Hello new world!\" ~alice/.config/foo")
      '';
  }