-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflake.nix
More file actions
230 lines (218 loc) · 8.28 KB
/
flake.nix
File metadata and controls
230 lines (218 loc) · 8.28 KB
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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake {inherit inputs;} ({config, ...}: let
flakePartsConfig = config;
in {
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
debug = true;
flake = {
nixosModules = {
ouro = {
config,
lib,
pkgs,
...
}: {
options = let
inherit (lib) types;
in {
vpnNamespaces = lib.mkOption {
type = types.attrsOf (types.submodule (vpnNamespaceModule @ {name, ...}: {
options = {
ouro = lib.mkOption {
default = {};
type = types.submodule (ouroSubmodule: {
options = {
enable = lib.mkEnableOption "ouro module subsystem for vpnNamespaces";
gateway = lib.mkOption {
type = types.str;
default = "10.2.0.1";
description = ''
Gateway IP Address of VPN Subnet.
Use `sudo ip netns exec ${name} bash -c "ip a"` to find out.
For Proton, default is `10.2.0.1`
'';
};
interface = lib.mkOption {
type = types.str;
default = "${name}0";
description = ''
Interface of the VPN connection in the confinement.
Use `sudo ip netns exec ${name} bash -c "ip a"` to find out.
Usually should be `${name}0`, but do double-check it.
'';
};
slskd = lib.mkOption {
default = {};
type = types.submodule {
options = {
enable = lib.mkEnableOption "Soulseek support";
};
};
};
transmission = lib.mkOption {
default = {};
type = types.submodule {
options = {
enable = lib.mkEnableOption "Soulseek support";
# TODO: needs modificaitons in `ouro` itself
# to allow loading from files
# rpc_file = ...
RPC_USER = lib.mkOption {
type = types.str;
};
RPC_PASS = lib.mkOption {
type = types.str;
};
ADDRESS = lib.mkOption {
type = types.str;
default = "127.0.0.1:9091";
description = ''
The address and port that ouro will provide to `transmission-remote`
for it to connect to. Can be <IP>:<Port> or just <IP>.
'';
};
};
};
};
};
});
};
};
}));
};
};
# TODO(reo101): assert that only one of `slskd` and `transmission` are enabled
# slskd.enable -> !transmission.enable
# transmission.enable -> !slskd.enable
config = {
systemd.services = lib.pipe config.vpnNamespaces [
# name = { ..., ouro = { STUFF }, ... }
(lib.mapAttrs (vpnNamespace: vpnNamespaceConfig: vpnNamespaceConfig.ouro))
# name = { STUFF }
(lib.filterAttrs (vpnNamespace: ouroConfig: ouroConfig.enable))
# name = { enable = true, STUFF }
(lib.mapAttrs' (vpnNamespace: ouroConfig:
lib.nameValuePair "ouro-${vpnNamespace}" {
description = ''
Ouro stuff for ${vpnNamespace}
'';
wantedBy = ["multi-user.target"];
after = [ "slskd.service" "${vpnNamespace}.service" ];
environment = {
# TODO(reo101): toggle with a module option
"RUST_LOG" = "debug";
};
path = [
pkgs.iptables
] ++ lib.optionals ouroConfig.transmission.enable [
# NOTE: used for `transmission-remote`
config.services.transmission.package
];
serviceConfig = let
inherit
(flakePartsConfig.allSystems.${pkgs.hostPlatform.system}.packages)
ouro
;
inherit
(ouroConfig)
gateway
interface
;
ouroArgs = if ouroConfig.slskd.enable then
"slskd"
else if ouroConfig.transmission.enable then
"transmission --rpc-user ${ouroConfig.transmission.RPC_USER} --rpc-pass ${ouroConfig.transmission.RPC_PASS} --address ${ouroConfig.transmission.ADDRESS}"
else
throw "Either `slskd` or `transmission` need to be enabled!";
in {
ExecStart = ''
${lib.getExe' pkgs.iproute2 "ip"} netns exec ${vpnNamespace} \
${lib.getExe pkgs.bashNonInteractive} -c "${lib.getExe ouro} --gateway ${gateway} --vpn-interface ${interface} ${ouroArgs}"
'';
Restart = "always";
};
vpnConfinement = {
enable = true;
inherit vpnNamespace;
};
}))
];
};
};
default = config.flake.nixosModules.ouro;
};
};
perSystem = {
config,
self',
inputs',
pkgs,
lib,
system,
...
}: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
inputs.rust-overlay.overlays.default
];
};
packages = {
default = pkgs.rustPlatform.buildRustPackage {
pname = "ouro";
version = "0.1.5";
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./src
./Cargo.toml
./Cargo.lock
];
};
cargoLock = {
lockFile = ./Cargo.lock;
};
nativeBuildInputs = with pkgs; [
pkg-config
];
meta = {
mainProgram = "ouro";
};
};
ouro = self'.packages.default;
};
devShells = {
default = pkgs.mkShell rec {
buildInputs = with pkgs; [
(rust-bin.stable.latest.default.override (oldAttrs: {
extensions =
(oldAttrs.extensions or [])
++ [
"rust-analyzer"
];
}))
pkg-config
clang
];
shellHook = ''
export LD_LIBRARY_PATH="${pkgs.lib.makeLibraryPath buildInputs}"
'';
};
};
};
});
}