Skip to content

Commit 38d57f5

Browse files
committed
lorri: add darwin daemon
Add a darwin daemon for lorri
1 parent ea164b7 commit 38d57f5

File tree

1 file changed

+145
-117
lines changed

1 file changed

+145
-117
lines changed

modules/services/lorri.nix

Lines changed: 145 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -31,124 +31,152 @@ in
3131
};
3232
};
3333

34-
config = lib.mkIf cfg.enable {
35-
assertions = [
36-
(lib.hm.assertions.assertPlatform "services.lorri" pkgs lib.platforms.linux)
37-
];
38-
39-
home.packages = [ cfg.package ];
40-
41-
systemd.user = {
42-
services.lorri = {
43-
Unit = {
44-
Description = "lorri build daemon";
45-
Requires = "lorri.socket";
46-
After = "lorri.socket";
47-
RefuseManualStart = true;
34+
config = lib.mkIf cfg.enable (
35+
let
36+
path =
37+
with pkgs;
38+
lib.makeSearchPath "bin" [
39+
cfg.nixPackage
40+
gitMinimal
41+
gnutar
42+
gzip
43+
];
44+
in
45+
lib.mkMerge [
46+
{
47+
home.packages = [ cfg.package ];
48+
}
49+
(lib.mkIf pkgs.stdenv.isDarwin {
50+
warnings =
51+
if cfg.enableNotifications then
52+
[
53+
"services.lorri.enableNotifications is not currently supported for Darwin."
54+
]
55+
else
56+
[ ];
57+
launchd.agents.lorri = {
58+
enable = true;
59+
config = {
60+
ProgramArguments = [
61+
"${cfg.package}/bin/lorri"
62+
"daemon"
63+
];
64+
65+
EnvironmentVariables = {
66+
PATH = "${path}";
67+
};
68+
69+
RunAtLoad = true;
70+
KeepAlive = {
71+
Crashed = true;
72+
SuccessfulExit = false;
73+
};
74+
};
4875
};
49-
50-
Service = {
51-
ExecStart = "${cfg.package}/bin/lorri daemon";
52-
PrivateTmp = true;
53-
ProtectSystem = "strict";
54-
ProtectHome = "read-only";
55-
ReadWritePaths = [
56-
# /run/user/1000 for the socket
57-
"%t"
58-
# Needs to update own cache
59-
"%C/lorri"
60-
# Needs %C/nix/fetcher-cache-v1.sqlite
61-
"%C/nix"
62-
];
63-
CacheDirectory = [ "lorri" ];
64-
Restart = "on-failure";
65-
Environment =
66-
let
67-
path =
68-
with pkgs;
69-
lib.makeSearchPath "bin" [
70-
cfg.nixPackage
71-
gitMinimal
72-
gnutar
73-
gzip
74-
];
75-
in
76-
[ "PATH=${path}" ];
77-
};
78-
};
79-
80-
sockets.lorri = {
81-
Unit = {
82-
Description = "Socket for lorri build daemon";
83-
};
84-
85-
Socket = {
86-
ListenStream = "%t/lorri/daemon.socket";
87-
RuntimeDirectory = "lorri";
88-
};
89-
90-
Install = {
91-
WantedBy = [ "sockets.target" ];
92-
};
93-
};
94-
95-
services.lorri-notify = lib.mkIf cfg.enableNotifications {
96-
Unit = {
97-
Description = "lorri build notifications";
98-
After = "lorri.service";
99-
Requires = "lorri.service";
100-
};
101-
102-
Service = {
103-
# Don't start until lorri daemon is actually running
104-
ExecStartPre = pkgs.writeShellScript "lorri-notify-check" ''
105-
lorri info --shell-file . | grep 'Lorri Daemon Status:.*running'
106-
'';
107-
RestartSec = "5s";
108-
109-
ExecStart =
110-
let
111-
jqFile = ''
112-
(
113-
(.Started? | values | ["Build starting", .nix_file, "emblem-synchronizing"]),
114-
(.Completed? | values | ["Build complete", .nix_file, "checkmark"]),
115-
(.Failure? | values | ["Build failed", .nix_file, "dialog-error"])
116-
)
117-
| @tsv
118-
'';
119-
120-
notifyScript = pkgs.writeShellScript "lorri-notify" ''
121-
set -o pipefail
122-
lorri internal stream-events --kind live \
123-
| jq --unbuffered -r '${jqFile}' \
124-
| while IFS=$'\t' read -r status nixFile icon; do
125-
notify-send --app-name "Lorri" --hint=int:transient:1 \
126-
--icon "$icon" "$status" "$nixFile"
127-
done
76+
})
77+
(lib.mkIf pkgs.stdenv.Linux {
78+
systemd.user = {
79+
services.lorri = {
80+
Unit = {
81+
Description = "lorri build daemon";
82+
Requires = "lorri.socket";
83+
After = "lorri.socket";
84+
RefuseManualStart = true;
85+
};
86+
87+
Service = {
88+
ExecStart = "${cfg.package}/bin/lorri daemon";
89+
PrivateTmp = true;
90+
ProtectSystem = "strict";
91+
ProtectHome = "read-only";
92+
ReadWritePaths = [
93+
# /run/user/1000 for the socket
94+
"%t"
95+
# Needs to update own cache
96+
"%C/lorri"
97+
# Needs %C/nix/fetcher-cache-v1.sqlite
98+
"%C/nix"
99+
];
100+
CacheDirectory = [ "lorri" ];
101+
Restart = "on-failure";
102+
Environment = [ "PATH=${path}" ];
103+
};
104+
};
105+
106+
sockets.lorri = {
107+
Unit = {
108+
Description = "Socket for lorri build daemon";
109+
};
110+
111+
Socket = {
112+
ListenStream = "%t/lorri/daemon.socket";
113+
RuntimeDirectory = "lorri";
114+
};
115+
116+
Install = {
117+
WantedBy = [ "sockets.target" ];
118+
};
119+
};
120+
121+
services.lorri-notify = lib.mkIf cfg.enableNotifications {
122+
Unit = {
123+
Description = "lorri build notifications";
124+
After = "lorri.service";
125+
Requires = "lorri.service";
126+
};
127+
128+
Service = {
129+
# Don't start until lorri daemon is actually running
130+
ExecStartPre = pkgs.writeShellScript "lorri-notify-check" ''
131+
lorri info --shell-file . | grep 'Lorri Daemon Status:.*running'
128132
'';
129-
in
130-
toString notifyScript;
131-
Restart = "on-failure";
132-
Environment =
133-
let
134-
path = lib.makeSearchPath "bin" (
135-
with pkgs;
136-
[
137-
bash
138-
gnugrep
139-
jq
140-
libnotify
141-
cfg.package
142-
]
143-
);
144-
in
145-
"PATH=${path}";
133+
RestartSec = "5s";
134+
135+
ExecStart =
136+
let
137+
jqFile = ''
138+
(
139+
(.Started? | values | ["Build starting", .nix_file, "emblem-synchronizing"]),
140+
(.Completed? | values | ["Build complete", .nix_file, "checkmark"]),
141+
(.Failure? | values | ["Build failed", .nix_file, "dialog-error"])
142+
)
143+
| @tsv
144+
'';
145+
146+
notifyScript = pkgs.writeShellScript "lorri-notify" ''
147+
set -o pipefail
148+
lorri internal stream-events --kind live \
149+
| jq --unbuffered -r '${jqFile}' \
150+
| while IFS=$'\t' read -r status nixFile icon; do
151+
notify-send --app-name "Lorri" --hint=int:transient:1 \
152+
--icon "$icon" "$status" "$nixFile"
153+
done
154+
'';
155+
in
156+
toString notifyScript;
157+
Restart = "on-failure";
158+
Environment =
159+
let
160+
path = lib.makeSearchPath "bin" (
161+
with pkgs;
162+
[
163+
bash
164+
gnugrep
165+
jq
166+
libnotify
167+
cfg.package
168+
]
169+
);
170+
in
171+
"PATH=${path}";
172+
};
173+
174+
Install = {
175+
WantedBy = [ "lorri.service" ];
176+
};
177+
};
146178
};
147-
148-
Install = {
149-
WantedBy = [ "lorri.service" ];
150-
};
151-
};
152-
};
153-
};
179+
})
180+
]
181+
);
154182
}

0 commit comments

Comments
 (0)