From 0a02a10f709ba6e1922251e52fe0093b6e518ed9 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazarewski Date: Thu, 19 Dec 2024 12:57:36 +0100 Subject: [PATCH] nixos/netbird: support configurable state directory --- nixos/modules/services/networking/netbird.nix | 26 +++- pkgs/tools/networking/netbird/default.nix | 141 ++++++++++-------- 2 files changed, 104 insertions(+), 63 deletions(-) diff --git a/nixos/modules/services/networking/netbird.nix b/nixos/modules/services/networking/netbird.nix index c9a2251437c6a6..bc5f28aa09a0f3 100644 --- a/nixos/modules/services/networking/netbird.nix +++ b/nixos/modules/services/networking/netbird.nix @@ -40,6 +40,7 @@ let enum nullOr package + path port str submodule @@ -171,8 +172,9 @@ in type = attrsOf str; defaultText = literalExpression '' { - NB_CONFIG = "/var/lib/netbird-''${client.name}/config.json"; - NB_DAEMON_ADDR = "unix:///var/run/netbird-''${client.name}/sock"; + NB_STATE_DIR = client.dir.state; + NB_CONFIG = "''${client.dir.state}/config.json"; + NB_DAEMON_ADDR = "unix://''${client.dir.runtime}/sock"; NB_INTERFACE_NAME = client.interface; NB_LOG_FILE = mkOptionDefault "console"; NB_LOG_LEVEL = client.logLevel; @@ -321,12 +323,28 @@ in or inspect existing file for a complete list of available configurations. ''; }; + + dir.state = mkOption { + type = path; + default = "/var/lib/netbird-${client.name}"; + description = '' + A state directory used by Netbird client to store `config.json`, `state.json` & `resolv.conf`. + ''; + }; + dir.runtime = mkOption { + type = path; + default = "/var/run/netbird-${client.name}"; + description = '' + A runtime directory used by Netbird client. + ''; + }; }; config.environment = { - NB_CONFIG = "/var/lib/netbird-${client.name}/config.json"; - NB_DAEMON_ADDR = "unix:///var/run/netbird-${client.name}/sock"; + NB_STATE_DIR = client.dir.state; + NB_CONFIG = "${client.dir.state}/config.json"; + NB_DAEMON_ADDR = "unix://${client.dir.runtime}/sock"; NB_INTERFACE_NAME = client.interface; NB_LOG_FILE = mkOptionDefault "console"; NB_LOG_LEVEL = client.logLevel; diff --git a/pkgs/tools/networking/netbird/default.nix b/pkgs/tools/networking/netbird/default.nix index ae39a1925e3983..f5e09931cf22b5 100644 --- a/pkgs/tools/networking/netbird/default.nix +++ b/pkgs/tools/networking/netbird/default.nix @@ -1,33 +1,38 @@ -{ stdenv -, lib -, nixosTests -, nix-update-script -, buildGoModule -, fetchFromGitHub -, installShellFiles -, pkg-config -, gtk3 -, libayatana-appindicator -, libX11 -, libXcursor -, libXxf86vm -, Cocoa -, IOKit -, Kernel -, UserNotifications -, WebKit -, ui ? false -, netbird-ui +{ + stdenv, + lib, + nixosTests, + nix-update-script, + buildGoModule, + fetchFromGitHub, + installShellFiles, + pkg-config, + gtk3, + libayatana-appindicator, + libX11, + libXcursor, + libXxf86vm, + Cocoa, + IOKit, + Kernel, + UserNotifications, + WebKit, + fetchpatch2, + ui ? false, + netbird-ui, }: let modules = - if ui then { - "client/ui" = "netbird-ui"; - } else { - client = "netbird"; - management = "netbird-mgmt"; - signal = "netbird-signal"; - }; + if ui then + { + "client/ui" = "netbird-ui"; + } + else + { + client = "netbird"; + management = "netbird-mgmt"; + signal = "netbird-signal"; + }; in buildGoModule rec { pname = "netbird"; @@ -44,19 +49,21 @@ buildGoModule rec { nativeBuildInputs = [ installShellFiles ] ++ lib.optional ui pkg-config; - buildInputs = lib.optionals (stdenv.hostPlatform.isLinux && ui) [ - gtk3 - libayatana-appindicator - libX11 - libXcursor - libXxf86vm - ] ++ lib.optionals (stdenv.hostPlatform.isDarwin && ui) [ - Cocoa - IOKit - Kernel - UserNotifications - WebKit - ]; + buildInputs = + lib.optionals (stdenv.hostPlatform.isLinux && ui) [ + gtk3 + libayatana-appindicator + libX11 + libXcursor + libXxf86vm + ] + ++ lib.optionals (stdenv.hostPlatform.isDarwin && ui) [ + Cocoa + IOKit + Kernel + UserNotifications + WebKit + ]; subPackages = lib.attrNames modules; @@ -70,6 +77,14 @@ buildGoModule rec { # needs network access doCheck = false; + patches = [ + (fetchpatch2 { + # add support for NB_STATE_DIR see https://github.com/netbirdio/netbird/pull/3084 + url = "https://github.com/netbirdio/netbird/commit/eddff4258fc9d6c8be6afafb1e49c67a7fed7cfe.patch?full_index=1"; + sha256 = "sha256-8gCLl2qO4NcG7U4TKZiW/omWFoKrUURWtHxYrPf8SP8="; + }) + ]; + postPatch = '' # make it compatible with systemd's RuntimeDirectory substituteInPlace client/cmd/root.go \ @@ -78,26 +93,31 @@ buildGoModule rec { --replace-fail 'unix:///var/run/netbird.sock' 'unix:///var/run/netbird/sock' ''; - postInstall = lib.concatStringsSep "\n" - (lib.mapAttrsToList - (module: binary: '' - mv $out/bin/${lib.last (lib.splitString "/" module)} $out/bin/${binary} - '' + lib.optionalString (!ui) '' - installShellCompletion --cmd ${binary} \ - --bash <($out/bin/${binary} completion bash) \ - --fish <($out/bin/${binary} completion fish) \ - --zsh <($out/bin/${binary} completion zsh) - '') - modules) + lib.optionalString (stdenv.hostPlatform.isLinux && ui) '' - mkdir -p $out/share/pixmaps - cp $src/client/ui/netbird-systemtray-connected.png $out/share/pixmaps/netbird.png + postInstall = + lib.concatStringsSep "\n" ( + lib.mapAttrsToList ( + module: binary: + '' + mv $out/bin/${lib.last (lib.splitString "/" module)} $out/bin/${binary} + '' + + lib.optionalString (!ui) '' + installShellCompletion --cmd ${binary} \ + --bash <($out/bin/${binary} completion bash) \ + --fish <($out/bin/${binary} completion fish) \ + --zsh <($out/bin/${binary} completion zsh) + '' + ) modules + ) + + lib.optionalString (stdenv.hostPlatform.isLinux && ui) '' + mkdir -p $out/share/pixmaps + cp $src/client/ui/netbird-systemtray-connected.png $out/share/pixmaps/netbird.png - mkdir -p $out/share/applications - cp $src/client/ui/netbird.desktop $out/share/applications/netbird.desktop + mkdir -p $out/share/applications + cp $src/client/ui/netbird.desktop $out/share/applications/netbird.desktop - substituteInPlace $out/share/applications/netbird.desktop \ - --replace-fail "Exec=/usr/bin/netbird-ui" "Exec=$out/bin/netbird-ui" - ''; + substituteInPlace $out/share/applications/netbird.desktop \ + --replace-fail "Exec=/usr/bin/netbird-ui" "Exec=$out/bin/netbird-ui" + ''; passthru = { tests.netbird = nixosTests.netbird; @@ -110,7 +130,10 @@ buildGoModule rec { changelog = "https://github.com/netbirdio/netbird/releases/tag/v${version}"; description = "Connect your devices into a single secure private WireGuard®-based mesh network with SSO/MFA and simple access controls"; license = licenses.bsd3; - maintainers = with maintainers; [ vrifox saturn745 ]; + maintainers = with maintainers; [ + vrifox + saturn745 + ]; mainProgram = if ui then "netbird-ui" else "netbird"; }; }