Skip to content

Commit 245a57c

Browse files
committed
Finish initial NixOS compatibility and add big warnings
Signed-off-by: magic_rb <magic_rb@redalder.org>
1 parent 4354a46 commit 245a57c

File tree

12 files changed

+324
-153
lines changed

12 files changed

+324
-153
lines changed

examples/default.nix

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@
1212
nixng,
1313
}:
1414
let
15-
modifiedMakeSystem = { config ? {}, ... }@args: nglib.makeSystem ( args //{
16-
config = {
17-
nixos.acceptRisks = "I accept the risks";
15+
modifiedMakeSystem =
16+
{
17+
config ? { },
18+
specialArgs ? { },
19+
...
20+
}@args:
21+
nglib.makeSystem (
22+
args
23+
// {
24+
config = {
25+
nixos.acceptRisks = "I accept the risks";
1826

19-
imports = [
20-
config
21-
];
22-
};
23-
});
27+
imports = [ config ];
28+
};
29+
specialArgs = specialArgs // {
30+
__enableExperimentalNixOSCompatibility = true;
31+
};
32+
}
33+
);
2434

2535
modifiedNglib = nglib // {
2636
makeSystem = modifiedMakeSystem;
@@ -48,4 +58,10 @@ let
4858
"ntfy-sh" = ./ntfy-sh;
4959
};
5060
in
51-
nixpkgs.lib.mapAttrs (_: v: import v { inherit nixpkgs nixng; nglib = modifiedNglib; }) examples
61+
nixpkgs.lib.mapAttrs (
62+
_: v:
63+
import v {
64+
inherit nixpkgs nixng;
65+
nglib = modifiedNglib;
66+
}
67+
) examples

lib/make-system.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ let
2424
evaledModules = evalModules {
2525
specialArgs = specialArgs // {
2626
inherit nglib;
27+
__enableExperimentalNixOSCompatibility =
28+
specialArgs.__enableExperimentalNixOSCompatibility or false;
2729
};
2830

2931
modules =

lib/options.nix

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@
3838
getOptionFromPath' [ ] path options;
3939

4040
mkOptionsEqual =
41-
to: from:
41+
to: from: mapper:
4242
{ config, options, ... }:
4343
let
4444
fromOpt = nglib.getOptionFromPath from options;
4545
toOpt = nglib.getOptionFromPath to options;
4646

4747
prio = fromOpt.highestPrio or lib.defaultOverridePriority;
48-
defsWithPrio = map (lib.mkOverride prio) fromOpt.definitions;
48+
defsWithPrio = map (def: lib.mkOverride prio (mapper def)) fromOpt.definitions;
4949
in
5050
{
51-
config = lib.attrsets.setAttrByPath to (
52-
lib.mkMerge defsWithPrio
53-
);
51+
config = lib.attrsets.setAttrByPath to (lib.mkMerge defsWithPrio);
5452
options = lib.attrsets.setAttrByPath from (
5553
lib.mkOption { apply = x: lib.attrsets.getAttrFromPath to config; }
5654
);

modules/nixos/assertions.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
{ config, lib, ... }:
1+
{
2+
config,
3+
lib,
4+
nglib,
5+
...
6+
}:
27
{
38
options.nixos = lib.mkOption { type = lib.types.submodule { imports = [ ../assertions.nix ]; }; };
49

5-
config.assertions = config.nixos.assertions;
10+
imports = [
11+
(nglib.mkOptionsEqual [ "assertions" ] [
12+
"nixos"
13+
"assertions"
14+
] lib.id)
15+
];
616
}

modules/nixos/default.nix

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
{ lib, pkgs, ... }:
21
{
3-
imports = [
2+
lib,
3+
pkgs,
4+
config,
5+
__enableExperimentalNixOSCompatibility,
6+
...
7+
}:
8+
{
9+
imports = lib.optionals __enableExperimentalNixOSCompatibility ([
410
./systemd.nix
511
./nginx.nix
612
./users.nix
@@ -10,7 +16,7 @@
1016
./nix.nix
1117
./meta.nix
1218
./networking.nix
13-
];
19+
]);
1420

1521
options.nixos = lib.mkOption {
1622
type = lib.types.submodule {
@@ -31,4 +37,19 @@
3137
};
3238
default = { };
3339
};
40+
41+
config.assertions = [
42+
{
43+
assertion = (config.nixos.acceptRisks == "I accept the risks");
44+
message = ''
45+
NixOS module compatibility is highly experimental, severely unfinished and most definitely has
46+
functional and security bugs. Unless you know what you're doing and are willing to accept the risks
47+
reconsider it's usage. To signify you are aware of these risks, set the option
48+
`config.nixos.acceptRisks` to `"I accept the risks"`.
49+
50+
If you run into any of the aforementioned deficiencies please reach out on Matrix at
51+
`#nixng:matrix.redalder.org`.
52+
'';
53+
}
54+
];
3455
}

modules/nixos/networking.nix

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{ lib, config, ... }:
22
{
33
options = {
4-
nixos.networking.hostName = lib.mkOption { type = lib.types.str; };
5-
};
6-
7-
config = {
8-
nixos.networking.hostName = "buildbot";
4+
nixos.networking.hostName = lib.mkOption {
5+
type = lib.types.str;
6+
description = ''
7+
Machine hostname, this has currently no effect on NixNG and is completely
8+
local to the NixOS compatibility layer.
9+
'';
10+
default = "unnamed";
11+
};
912
};
1013
}

modules/nixos/nginx.nix

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -127,110 +127,109 @@ in
127127
imports = [
128128
(nglib.mkOptionsEqual
129129
[
130-
"nixos"
131130
"services"
132131
"nginx"
133132
"enable"
134133
]
135134
[
135+
"nixos"
136136
"services"
137137
"nginx"
138138
"enable"
139139
]
140+
lib.id
140141
)
141142
];
142143

143144
config = {
144-
services.nginx = lib.mkIf config.services.nginx.enable (
145-
nglib.errorExperimentalNixOS config {
146-
envsubst = true;
147-
configuration = lib.singleton {
148-
daemon = "off";
149-
worker_processes = 8;
150-
user = "nginx";
151-
152-
events."" = {
153-
use = "epoll";
154-
worker_connections = 512;
155-
};
156-
157-
error_log = [
158-
"/dev/stderr"
159-
"warn"
160-
];
161-
162-
pid = "/nginx.pid";
163-
164-
http."" =
165-
[
166-
{
167-
server_tokens = "off";
168-
include = [ [ "${pkgs.nginx}/conf/mime.types" ] ];
169-
charset = "utf-8";
170-
access_log = [
171-
"/dev/stdout"
172-
"combined"
173-
];
145+
services.nginx = lib.mkIf config.services.nginx.enable ({
146+
envsubst = true;
147+
configuration = lib.singleton {
148+
daemon = "off";
149+
worker_processes = 8;
150+
user = "nginx";
151+
152+
events."" = {
153+
use = "epoll";
154+
worker_connections = 512;
155+
};
174156

175-
# $connection_upgrade is used for websocket proxying
176-
map."$$http_upgrade $$connection_upgrade" = {
177-
default = "upgrade";
178-
"''" = "close";
179-
};
180-
}
181-
]
182-
++ (lib.optionals cfg.recommendedProxySettings [
183-
{
184-
proxy_redirect = "off";
185-
proxy_connect_timeout = cfg.proxyTimeout;
186-
proxy_send_timeout = cfg.proxyTimeout;
187-
proxy_read_timeout = cfg.proxyTimeout;
188-
proxy_http_version = "1.1";
189-
# don't let clients close the keep-alive connection to upstream. See the nginx blog for details:
190-
# https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives
191-
proxy_set_header = [
192-
"Connection"
193-
"''"
157+
error_log = [
158+
"/dev/stderr"
159+
"warn"
160+
];
161+
162+
pid = "/nginx.pid";
163+
164+
http."" =
165+
[
166+
{
167+
server_tokens = "off";
168+
include = [ [ "${pkgs.nginx}/conf/mime.types" ] ];
169+
charset = "utf-8";
170+
access_log = [
171+
"/dev/stdout"
172+
"combined"
173+
];
174+
175+
# $connection_upgrade is used for websocket proxying
176+
map."$$http_upgrade $$connection_upgrade" = {
177+
default = "upgrade";
178+
"''" = "close";
179+
};
180+
}
181+
]
182+
++ (lib.optionals cfg.recommendedProxySettings [
183+
{
184+
proxy_redirect = "off";
185+
proxy_connect_timeout = cfg.proxyTimeout;
186+
proxy_send_timeout = cfg.proxyTimeout;
187+
proxy_read_timeout = cfg.proxyTimeout;
188+
proxy_http_version = "1.1";
189+
# don't let clients close the keep-alive connection to upstream. See the nginx blog for details:
190+
# https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives
191+
proxy_set_header = [
192+
"Connection"
193+
"''"
194+
];
195+
}
196+
recommendedProxyConfig
197+
])
198+
++ (lib.flip lib.mapAttrsToList cfg.virtualHosts (
199+
server_name: server: {
200+
server."" = {
201+
listen = [
202+
"80"
203+
"http2"
194204
];
195-
}
196-
recommendedProxyConfig
197-
])
198-
++ (lib.flip lib.mapAttrsToList cfg.virtualHosts (
199-
server_name: server: {
200-
server."" = {
201-
listen = [
202-
"80"
203-
"http2"
204-
];
205-
inherit server_name;
206-
207-
location = lib.flip lib.mapAttrs server.locations (
208-
location: settings: [
209-
(lib.optionalAttrs (
210-
settings.proxyPass != null && cfg.recommendedProxySettings
211-
) recommendedProxyConfig)
212-
(lib.optionalAttrs settings.proxyWebsockets {
213-
proxy_http_version = "1.1";
214-
proxy_set_header = [
215-
[
216-
"Upgrade"
217-
"$$http_upgrade"
218-
]
219-
[
220-
"Connection"
221-
"$$connection_upgrade"
222-
]
223-
];
224-
})
225-
settings.extraConfig
226-
(lib.optionalAttrs (settings.proxyPass != null) { proxy_pass = settings.proxyPass; })
227-
]
228-
);
229-
};
230-
}
231-
));
232-
};
233-
}
234-
);
205+
inherit server_name;
206+
207+
location = lib.flip lib.mapAttrs server.locations (
208+
location: settings: [
209+
(lib.optionalAttrs (
210+
settings.proxyPass != null && cfg.recommendedProxySettings
211+
) recommendedProxyConfig)
212+
(lib.optionalAttrs settings.proxyWebsockets {
213+
proxy_http_version = "1.1";
214+
proxy_set_header = [
215+
[
216+
"Upgrade"
217+
"$$http_upgrade"
218+
]
219+
[
220+
"Connection"
221+
"$$connection_upgrade"
222+
]
223+
];
224+
})
225+
settings.extraConfig
226+
(lib.optionalAttrs (settings.proxyPass != null) { proxy_pass = settings.proxyPass; })
227+
]
228+
);
229+
};
230+
}
231+
));
232+
};
233+
});
235234
};
236235
}

modules/nixos/nix.nix

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
{ lib, config, ... }:
21
{
3-
options = {
4-
nixos.nix = {
5-
settings = lib.mkOption {
6-
type = lib.types.unspecified;
7-
default = { };
8-
};
9-
};
10-
};
11-
12-
config = {
13-
nix.config = config.nixos.nix.settings;
14-
};
2+
nglib,
3+
lib,
4+
config,
5+
...
6+
}:
7+
{
8+
imports = [
9+
(nglib.mkOptionsEqual
10+
[
11+
"nix"
12+
"config"
13+
]
14+
[
15+
"nixos"
16+
"nix"
17+
"config"
18+
]
19+
lib.id
20+
)
21+
];
1522
}

0 commit comments

Comments
 (0)