-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
244 lines (222 loc) · 7.72 KB
/
flake.nix
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
inputs = {
nixpkgs = {
url = "github:nixos/nixpkgs/nixpkgs-unstable";
follows = "shellswain/nixpkgs";
};
flake-utils = {
url = "github:numtide/flake-utils";
follows = "shellswain/flake-utils";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
follows = "shellswain/flake-compat";
};
shellswain.url = "github:abathur/shellswain/v0.3.0";
bats-require = {
url = "github:abathur/bats-require";
follows = "shellswain/bats-require";
};
};
outputs = { self, nixpkgs, flake-utils, flake-compat, shellswain, bats-require }:
let
sharedInit = pkgs: ''
source ${pkgs.hag}/bin/hag.bash
'';
sharedOptions = userHome: pkgs: with nixpkgs.lib; {
enable = mkEnableOption "hag";
# enable = mkOption {
# description = "Whether to enable hag.";
# default = false;
# type = types.bool;
# };
init = mkOption {
default = true;
type = types.bool;
description = ''
Whether to auto-add interactiveShellInit for hag.
Disable this if you need to control where and how
hag is sourced.
'';
};
package = mkPackageOption pkgs "hag" { };
# package = mkOption {
# description = "Hag package to use.";
# default = pkgs.hag;
# defaultText = "pkgs.hag";
# type = types.package;
# };
user = mkOption {
type = types.str;
default = "root";
description = ''
User to aggregate history for.
'';
};
# TODO: see if other modules do anything obvious about XDG?
dataDir = mkOption {
type = types.str;
default = "${userHome}/.config/hag";
description = ''
Data directory for hag.
'';
};
# TODO: this is only wired up for darwin atm, but I'm mainly using
# it as a debug affordance anyways--so perhaps I should
# weigh whether it should exist or be unset by default?
logFile = mkOption {
type = types.str;
default = "${userHome}/hag.log";
description = ''
Logfile for hag.
'';
};
};
testUser = "user1";
testPassword = "password1234";
in
{
overlays.default = nixpkgs.lib.composeExtensions shellswain.overlays.default (final: prev: {
hag = final.callPackage ./hag.nix { };
});
darwinModules.hag = { config, pkgs, lib, ... }:
let
cfg = config.programs.hag;
userHome = config.users.users.${cfg.user}.home;
in {
options.programs.hag = (sharedOptions userHome pkgs);
# TODO: either update to track nixos, or factor common parts back out again?
config = lib.mkIf cfg.enable {
launchd.user.agents.hag = {
command = "${cfg.package}/bin/hagd.bash '${cfg.package}' '${cfg.dataDir}'";
serviceConfig = {
StandardOutPath = builtins.toPath "${cfg.logFile}";
StandardErrorPath = builtins.toPath "${cfg.logFile}";
RunAtLoad = true;
KeepAlive = true;
};
};
# TODO: unlike the nixos version, this is untested until you try it in nix darwin
programs.bash.interactiveShellInit = lib.optionalString cfg.init (sharedInit pkgs);
};
};
# darwinModules.default = self.darwinModules.${system}.hag;
nixosModules.hag = { config, pkgs, lib, ... }:
let
cfg = config.programs.hag;
userHome = config.users.users.${cfg.user}.home;
in {
options.programs.hag = (sharedOptions userHome pkgs);
config = lib.mkIf cfg.enable {
systemd.services.hag = {
description = "Shell-hag";
# TODO: IDK about wantedBy and after
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${cfg.package}/bin/hagd.bash '${cfg.package}' '${cfg.dataDir}'";
Restart = "on-failure";
User = "${cfg.user}";
};
};
programs.bash.interactiveShellInit = lib.optionalString cfg.init (sharedInit pkgs);
};
};
# nixosModules.default = self.darwinModules.${system}.hag;
# shell = ./shell.nix;
checks.x86_64-linux.integration = let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlays.default ];
};
in pkgs.nixosTest {
name = "hag-integration";
nodes.system1 = { config, pkgs, ... }: {
imports = [
self.nixosModules.hag
];
programs.hag = {
user = "${testUser}";
enable = true;
};
# programs.zsh.interactiveShellInit = ''
# source ${pkgs.hag}/bin/hag.bash
# '';
users = {
mutableUsers = false;
users = {
"${testUser}" = {
isNormalUser = true;
password = "${testPassword}";
};
};
};
environment.systemPackages = [
# TODO: could probably de-dupe this with hag's real
# test suite...
(pkgs.writeScriptBin "expect-bash" ''
#!${pkgs.expect}/bin/expect -df
spawn env TERM=xterm ${pkgs.bashInteractive}/bin/bash -i
expect "hag doesn't have a purpose; please set one:" {
send "porpoise\n"
expect "Should hag track the history for purpose 'porpoise'" {
send -- "y\r"
expect "hag is tracking history" {
send -- "uname -a\r"
expect "NixOS" {
send "echo bash displayed hag > rage\r"
send -- "exit\r"
}
}
}
} timeout {
send_user "\nbash failed to display hag\n"
exit 1
}
expect eof
'')
];
};
testScript = ''
system1.wait_for_unit("multi-user.target")
system1.wait_until_succeeds("pgrep -f 'agetty.*tty1'")
with subtest("open virtual console"):
# system1.fail("pgrep -f 'agetty.*tty2'")
system1.send_key("alt-f2")
system1.wait_until_succeeds("[ $(fgconsole) = 2 ]")
system1.wait_for_unit("getty@tty2.service")
system1.wait_until_succeeds("pgrep -f 'agetty.*tty2'")
with subtest("Log in as ${testUser} on new virtual console"):
system1.wait_until_tty_matches("2", "login: ")
system1.send_chars("${testUser}\n")
system1.wait_until_tty_matches("2", "login: ${testUser}")
system1.wait_until_succeeds("pgrep login")
system1.wait_until_tty_matches("2", "Password: ")
system1.send_chars("${testPassword}\n")
system1.wait_until_tty_matches("2", "$")
assert "bash displayed hag" in system1.succeed("su -l ${testUser} -c 'expect-bash'")
assert "uname" in system1.succeed("cat /home/${testUser}/.config/hag/porpoise/.bash")
assert "ingesting" in system1.succeed("journalctl -u hag.service")
'';
};
} // flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
bats-require.overlays.default
shellswain.overlays.default
self.overlays.default
];
};
in
{
packages = {
inherit (pkgs) hag;
default = pkgs.hag;
};
}
);
}