Skip to content

Commit b0671d6

Browse files
migrate nix package build process to crane (#876)
* update dependencies * try out crane for caching nix builds * cleanup system service * nix package cleanup * remove runtime-only deps from buildInputs * expose pnpmDeps via passthru * replace with lib; with explicit lib. prefixes * scope rust-overlay to devShells, add checks * fix nix dev shell craneLib wiring * package cleanup & service hardening * update dependencies --------- Co-authored-by: Jacek Chmielewski <jacek@defguard.net>
1 parent 847be11 commit b0671d6

8 files changed

Lines changed: 469 additions & 379 deletions

File tree

flake.lock

Lines changed: 22 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
nixpkgs.url = "nixpkgs";
44
flake-utils.url = "github:numtide/flake-utils";
55
rust-overlay.url = "github:oxalica/rust-overlay";
6+
crane.url = "github:ipetkov/crane";
67

78
# let git manage submodules
89
self.submodules = true;
@@ -25,26 +26,36 @@
2526
nixpkgs,
2627
flake-utils,
2728
rust-overlay,
29+
crane,
2830
...
2931
}:
3032
flake-utils.lib.eachDefaultSystem (system: let
31-
# add rust overlay
32-
pkgs = import nixpkgs {
33+
# Plain nixpkgs — used for packages and checks.
34+
pkgs = import nixpkgs {inherit system;};
35+
36+
# nixpkgs with rust-overlay — only needed for the dev shell, which uses
37+
# pkgs.rust-bin to get a customised Rust toolchain.
38+
devPkgs = import nixpkgs {
3339
inherit system;
3440
overlays = [rust-overlay.overlays.default];
3541
};
42+
43+
craneLib = crane.mkLib pkgs;
3644
in {
3745
devShells.default = import ./nix/shell.nix {
38-
inherit pkgs;
46+
pkgs = devPkgs;
47+
inherit crane;
3948
};
4049

4150
packages.default = pkgs.callPackage ./nix/package.nix {
42-
inherit pkgs;
51+
inherit pkgs craneLib;
4352
};
4453

54+
checks.default = self.packages.${system}.default;
55+
4556
formatter = pkgs.alejandra;
4657
})
4758
// {
48-
nixosModules.default = import ./nix/nixos-module.nix;
59+
nixosModules.default = import ./nix/nixos-module.nix {mkCraneLib = crane.mkLib;};
4960
};
5061
}

nix/nixos-module.nix

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
1-
{
1+
{mkCraneLib}: {
22
config,
33
lib,
44
pkgs,
55
...
6-
}:
7-
with lib; let
8-
defguard-client = pkgs.callPackage ./package.nix {};
6+
}: let
7+
craneLib = mkCraneLib pkgs;
8+
defguard-client = pkgs.callPackage ./package.nix {inherit pkgs craneLib;};
99
cfg = config.programs.defguard-client;
1010
in {
1111
options.programs.defguard-client = {
12-
enable = mkEnableOption "Defguard VPN client and service";
12+
enable = lib.mkEnableOption "Defguard VPN client and service";
1313

14-
package = mkOption {
15-
type = types.package;
14+
package = lib.mkOption {
15+
type = lib.types.package;
1616
default = defguard-client;
1717
description = "defguard-client package to use";
1818
};
1919

20-
logLevel = mkOption {
21-
type = types.str;
20+
logLevel = lib.mkOption {
21+
type = lib.types.str;
2222
default = "info";
2323
description = "Log level for defguard-service";
2424
};
2525

26-
statsPeriod = mkOption {
27-
type = types.int;
26+
statsPeriod = lib.mkOption {
27+
type = lib.types.int;
2828
default = 30;
2929
description = "Interval in seconds for interface statistics updates";
3030
};
3131
};
3232

33-
config = mkIf cfg.enable {
34-
# Add client package
33+
config = lib.mkIf cfg.enable {
3534
environment.systemPackages = [cfg.package];
3635

37-
# Setup systemd service for the intrerface management daemon
3836
systemd.services.defguard-service = {
3937
description = "Defguard VPN Service";
38+
documentation = ["https://docs.defguard.net"];
4039
wantedBy = ["multi-user.target"];
4140
wants = ["network-online.target"];
4241
after = ["network-online.target"];
4342
serviceConfig = {
44-
ExecStart = "${cfg.package}/bin/defguard-service --log-level ${cfg.logLevel} --stats-period ${toString cfg.statsPeriod}";
45-
ExecReload = "/bin/kill -HUP $MAINPID";
4643
Group = "defguard";
47-
Restart = "on-failure";
48-
RestartSec = 2;
44+
ExecStart = "${cfg.package}/bin/defguard-service --log-level ${cfg.logLevel} --stats-period ${toString cfg.statsPeriod}";
45+
ExecReload = "kill -HUP $MAINPID";
4946
KillMode = "process";
5047
KillSignal = "SIGINT";
5148
LimitNOFILE = 65536;
5249
LimitNPROC = "infinity";
50+
Restart = "on-failure";
51+
RestartSec = 2;
5352
TasksMax = "infinity";
5453
OOMScoreAdjust = -1000;
54+
# Security hardening
55+
NoNewPrivileges = true;
56+
PrivateTmp = true;
57+
ProtectControlGroups = true;
58+
ProtectKernelModules = true;
59+
RestrictRealtime = true;
60+
LockPersonality = true;
5561
};
5662
};
5763

58-
# Make sure the defguard group exists
5964
users.groups.defguard = {};
6065
};
6166
}

0 commit comments

Comments
 (0)