-
Notifications
You must be signed in to change notification settings - Fork 153
/
flake.nix
242 lines (212 loc) · 7.63 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
{
description = "picsum.photos";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = inputs@{
self,
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
] (system:
let pkgs = nixpkgs.legacyPackages.${system}; in {
packages = rec {
default = everything;
everything = pkgs.symlinkJoin {
name = "picsum-photos-composite";
paths = [
picsum-photos
image-service
];
};
picsum-photos = pkgs.buildGo122Module {
name = "picsum-photos";
src = ./.;
CGO_ENABLED = 0;
subPackages = ["cmd/picsum-photos"];
doCheck = false; # Prevent make test from being ran
vendorHash = (pkgs.lib.fileContents ./go.mod.sri);
nativeBuildInputs = with pkgs; [
tailwindcss
];
preBuild = ''
go generate ./...
'';
};
image-service = pkgs.buildGo122Module {
name = "image-service";
src = ./.;
subPackages = ["cmd/image-service"];
doCheck = false; # Prevent make test from being ran
vendorHash = (pkgs.lib.fileContents ./go.mod.sri);
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = with pkgs; [
vips
];
};
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
go_1_22
gotools
go-tools
gopls
delve
];
};
}
) // {
nixosModules.default = { config, lib, pkgs, ... }:
with lib;
let cfg = config.picsum-photos.services;
in {
options.picsum-photos.services = {
picsum-photos = {
enable = mkEnableOption "Enable the picsum-photos service";
logLevel = mkOption {
type = with types; enum [ "debug" "info" "warn" "error" "dpanic" "panic" "fatal" ];
example = "debug";
default = "info";
description = "log level";
};
domain = mkOption {
type = types.str;
description = "Domain to listen to";
};
sockPath = mkOption rec {
type = types.path;
default = "/run/picsum-photos/picsum-photos.sock";
example = default;
description = "Unix domain socket to listen on";
};
environmentFile = mkOption {
type = types.path;
description = "Environment file";
};
databaseFilePath = mkOption rec {
type = types.path;
default = "/var/lib/picsum-photos/image-manifest.json";
example = default;
description = "Image database file path";
};
};
image-service = {
enable = mkEnableOption "Enable the image-service service";
logLevel = mkOption {
type = with types; enum [ "debug" "info" "warn" "error" "dpanic" "panic" "fatal" ];
example = "debug";
default = "info";
description = "log level";
};
workers = mkOption rec {
type = types.number;
default = 16;
example = default;
description = "worker queue concurrency";
};
domain = mkOption {
type = types.str;
description = "Domain to listen to";
};
sockPath = mkOption rec {
type = types.path;
default = "/run/image-service/image-service.sock";
example = default;
description = "Unix domain socket to listen on";
};
environmentFile = mkOption {
type = types.path;
description = "Environment file";
};
storagePath = mkOption rec {
type = types.path;
default = "/var/lib/image-service";
example = default;
description = "Storage path";
};
};
};
config = mkMerge([
(mkIf cfg.picsum-photos.enable {
users.groups.picsum-photos = {};
users.users.picsum-photos = {
createHome = true;
isSystemUser = true;
group = "picsum-photos";
home = "/var/lib/picsum-photos";
};
systemd.services.picsum-photos = {
description = "picsum-photos";
wantedBy = [ "multi-user.target" ];
script = ''
exec ${self.packages.${pkgs.system}.picsum-photos}/bin/picsum-photos \
-log-level=${cfg.picsum-photos.logLevel} \
-listen=${cfg.picsum-photos.sockPath} \
-database-file-path=${cfg.picsum-photos.databaseFilePath}
'';
serviceConfig = {
EnvironmentFile = cfg.picsum-photos.environmentFile;
User = "picsum-photos";
Group = "picsum-photos";
Restart = "always";
RestartSec = "30s";
WorkingDirectory = "/var/lib/picsum-photos";
RuntimeDirectory = "picsum-photos";
RuntimeDirectoryMode = "0770";
UMask = "007";
};
};
services.nginx.virtualHosts."${cfg.picsum-photos.domain}" = {
locations."/" = {
proxyPass = "http://unix:${cfg.picsum-photos.sockPath}";
};
};
})
(mkIf cfg.image-service.enable {
users.groups.image-service = {};
users.users.image-service = {
createHome = true;
isSystemUser = true;
group = "image-service";
home = "/var/lib/image-service";
};
systemd.services.image-service = {
description = "image-service";
wantedBy = [ "multi-user.target" ];
script = ''
exec ${self.packages.${pkgs.system}.image-service}/bin/image-service \
-log-level=${cfg.image-service.logLevel} \
-listen=${cfg.image-service.sockPath} \
-storage-path=${cfg.image-service.storagePath} \
-workers=${toString cfg.image-service.workers}
'';
serviceConfig = {
EnvironmentFile = cfg.image-service.environmentFile;
User = "image-service";
Group = "image-service";
Restart = "always";
RestartSec = "30s";
WorkingDirectory = "/var/lib/image-service";
RuntimeDirectory = "image-service";
RuntimeDirectoryMode = "0770";
UMask = "007";
};
};
services.nginx.virtualHosts."${cfg.image-service.domain}" = {
locations."/" = {
proxyPass = "http://unix:${cfg.image-service.sockPath}";
};
};
})
]);
};
};
}