Skip to content

Commit

Permalink
nixos/netbird: support configurable state directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarewk committed Dec 27, 2024
1 parent 2480570 commit 0a02a10
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 63 deletions.
26 changes: 22 additions & 4 deletions nixos/modules/services/networking/netbird.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ let
enum
nullOr
package
path
port
str
submodule
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
141 changes: 82 additions & 59 deletions pkgs/tools/networking/netbird/default.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand All @@ -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 \
Expand All @@ -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;
Expand All @@ -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";
};
}

0 comments on commit 0a02a10

Please sign in to comment.