Skip to content

nixos/netbird: harden and extend options - #287236

Merged
fricklerhandwerk merged 4 commits into
NixOS:masterfrom
nazarewk-iac:netbird-improvements
Jan 28, 2025
Merged

fricklerhandwerk merged 4 commits into
NixOS:masterfrom
nazarewk-iac:netbird-improvements

Conversation

@nazarewk

@nazarewk nazarewk commented Feb 8, 2024 •

Copy link
Copy Markdown
Member

Description of changes

I have recently extensively tested and fixed all features of Netbird in my own implementation of multi-instance Netbird installations.

While doing so I discovered another multi-instance implementation got merged into nixpkgs #246055 which is slightly different, but still a solid base to upstream the rest of my changes:

  • running as DynamicUser it's own user with minimal set of permissions
    • it was there before, but was lacking some of capabilities,
  • made some configurations situational
  • add more unmanaged interface configurations
  • quality of life improvements:
    • configure log level for each interface
    • optionally turn off starting during boot
    • openFirewall by default
    • add shortcuts/wrappers for each created instance

I think it's a pretty good time to upstream, because I will be extensively using it at work: just launched my first Colmena-managed NixOS into GCE.

There are plans to support multi-account connections on the same daemon in Q2/2024 (see the slack message), but it's not known what shape it will take at all.

I decided to implement following significant changes:

  • instances must specify a port they will be listening on as it doesn't make much sense to give an immediately conflicting default,
  • aliased tunnels to clients, because a word tunnel does not exist in Netbird's nomenclature (unlike some other VPNs) and is pretty misleading. Also clients.* play nicely with my plan to implement a server in near future.
  • skipped destructuring expressions (eg: {name, ...}: name -> client: client.name) because they make the code very hard to follow and update with increased number of options,

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions Bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Feb 8, 2024
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Feb 8, 2024
@nazarewk nazarewk changed the title nixos/netbird: run as DynamicUser with more configuration options nixos/netbird: bring back DynamicUser with more configuration options Feb 9, 2024
@nazarewk
nazarewk marked this pull request as draft February 9, 2024 08:43
@nazarewk nazarewk changed the title nixos/netbird: bring back DynamicUser with more configuration options nixos/netbird: harden and extend options Feb 9, 2024
@nazarewk
nazarewk force-pushed the netbird-improvements branch 5 times, most recently from 0cf761f to 4179661 Compare February 9, 2024 12:47
@nazarewk
nazarewk marked this pull request as ready for review February 9, 2024 12:54
nazarewk added a commit to nazarewk-iac/nix-configs that referenced this pull request Feb 9, 2024
Signed-off-by: Krzysztof Nazarewski <gpg@kdn.im>
@Tom-Hubrecht Tom-Hubrecht assigned mlvzk and unassigned mlvzk Feb 10, 2024
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
@nazarewk
nazarewk force-pushed the netbird-improvements branch from 4179661 to 0a1d920 Compare February 12, 2024 15:30
@nazarewk
nazarewk requested a review from misuzu February 12, 2024 15:33
@nazarewk
nazarewk force-pushed the netbird-improvements branch from 0a1d920 to add2bf7 Compare February 12, 2024 15:46
@github-actions github-actions Bot added 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes labels Feb 12, 2024
@nazarewk
nazarewk force-pushed the netbird-improvements branch from add2bf7 to 26373ef Compare February 12, 2024 20:39
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.md Outdated
Comment thread nixos/tests/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
Comment thread nixos/modules/services/networking/netbird.nix Outdated
@nazarewk
nazarewk force-pushed the netbird-improvements branch 3 times, most recently from a734f1e to 35a7c67 Compare February 13, 2024 11:08
@oddlama

oddlama commented May 18, 2024 •

Copy link
Copy Markdown
Member

When enabling ui.enable, only the netbird-<name> is added to the system path. The ui wrapper netbird-ui-<name> will not be easily accessible, except for the old netbird.enable where you add an explicit "as-default" wrapper.

And you currently cannot run more than one ui (if you have multiple clients), because the client wants to own /tmp/wiretrustee.pid (which is a bit weird), which especially causes problems when two users want to use netbird. The second user cannot start the ui because the file is already owned by the first user

@nazarewk

Copy link
Copy Markdown
Member Author

This new module unfortunately causes netbird to segfault on start, except when you use the old enable option. This seems to be because /etc/netbird/config.json contains correct addresses for management url and admin url, while configurations with other directories will have them default to null which causes a segfault because url structs are not expected to be null.
...
EDIT: The culprit actually seems to be related to the prestart script. If the config.json file does not exist yet, netbird will usually populate it using the correct values. But if it doesn't exist before running the pre-start script, it will be created containing only a subset of the required values, causing netbird to assume they should be unset or something.

might be caused by merging/lack of netbirdio/netbird#1586 , I'll simply rebase today and try to address your comments tomorrow.

@nazarewk

Copy link
Copy Markdown
Member Author

When enabling ui.enable, only the netbird-<name> is added to the system path. The ui wrapper netbird-ui-<name> will not be easily accessible, except for the old netbird.enable where you add an explicit "as-default" wrapper.

Works fine for me:

> which netbird-ui-priv netbird-ui-sc
/run/current-system/sw/bin/netbird-ui-priv
/run/current-system/sw/bin/netbird-ui-sc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better style to force the prefix in the relevant locations, otherwise /var/lib/${cfg.name} will always look problematic since it implies any name is possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds reasonable

@oddlama oddlama left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll have to adjust the release notes to apply to 24.11 now that 24.05 is out the door. But the important details look fine to me, so this is good to go from my side. We can iron out any possible details separately if necessary.

@oddlama

oddlama commented Jun 9, 2024

Copy link
Copy Markdown
Member

There are still changes to nixos/doc/manual/release-notes/rl-2405.section.md

@nazarewk

nazarewk commented Jun 9, 2024

Copy link
Copy Markdown
Member Author

There are still changes to nixos/doc/manual/release-notes/rl-2405.section.md

Yeah, the nixos manual command refused to pass, assumed it was expecting me to change it?

@oddlama

oddlama commented Jun 9, 2024

Copy link
Copy Markdown
Member

Yeah, the nixos manual command refused to pass, assumed it was expecting me to change it?

I guess it complains because you cannot edit old release notes

@nazarewk

Copy link
Copy Markdown
Member Author

Yeah, the nixos manual command refused to pass, assumed it was expecting me to change it?

I guess it complains because you cannot edit old release notes

it refused to pass without changes https://github.com/NixOS/nixpkgs/actions/runs/9399057023/job/25885722356

@PatrickDaG PatrickDaG left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module looks great. Hope it gets merged soon, been long enough.
I would like to test this more myself, but I'm working on a rewrite of the netbird-server modules right now and while I don't think they actually conflict, the merges do.

Comment thread nixos/doc/manual/release-notes/rl-2405.section.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to add more override files, or why do you first write the file in /etc and later iterate over the one file? I think it would be simpler to skip this and directly access the file from the nix-store in the derivation, skipping one indirection.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using those on some of my machines and while testing stuff.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the client.ui.enable option then be removed as it doesn't do anything?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probs would be better to use formatType.generate "config.json" cfg.config; since you're using the json format type already

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think that's necessary, I think the generation uses jq with a separate derivation underneath.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an disable-auto-connect flag but it seems to only apply to netbird up, the link however seems to have run stale as there is no mention of such a feature being planned as far as I can see.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta look into it, I'm using some form of this in my configs successfully

@fricklerhandwerk

Copy link
Copy Markdown
Contributor

I'd merge but NixOS tests seem to be broken or hanging? @Mic92 how to unblock this?

@oddlama

oddlama commented Jan 20, 2025

Copy link
Copy Markdown
Member

(And this still modifies old release notes)

@nazarewk

Copy link
Copy Markdown
Member Author

(And this still modifies old release notes)

any idea how to prevent that? nixpkgs is not activatable without this modification

@Mic92

Mic92 commented Jan 20, 2025

Copy link
Copy Markdown
Member

I'd merge but NixOS tests seem to be broken or hanging? @Mic92 how to unblock this?

Fixing the test? I am not using netbird and have currently other things on my list. Sorry.

@fricklerhandwerk

fricklerhandwerk commented Jan 20, 2025 •

Copy link
Copy Markdown
Contributor

I'm not sure if and how the test is even broken. All that's observable is that it's running seemingly forever, and I wondered how to re-trigger it or something. Is any of that ofborg stuff documented anywhere @dasJ?

@nazarewk

Copy link
Copy Markdown
Member Author

I'd merge but NixOS tests seem to be broken or hanging? @Mic92 how to unblock this?

Fixing the test? I am not using netbird and have currently other things on my list. Sorry.

I'll adress remaining things this week. Otherwise I didn't touch it apart from rebasing for months already.

@nazarewk

Copy link
Copy Markdown
Member Author

I have fixed the tests (I did some incompatible changes to the module on the way), but even though I've added option rename, the manual still doesn't build:
https://github.com/NixOS/nixpkgs/blob/c65c0c24c0b1fe683cea50f1c32893304cf0b1f7/nixos/modules/services/networking/netbird.nix#L75-L77

@nazarewk

nazarewk commented Jan 27, 2025 •

Copy link
Copy Markdown
Member Author

I have fixed the tests (I did some incompatible changes to the module on the way), but even though I've added option rename, the manual still doesn't build:

https://github.com/NixOS/nixpkgs/blob/c65c0c24c0b1fe683cea50f1c32893304cf0b1f7/nixos/modules/services/networking/netbird.nix#L75-L77

seems like there is some (actually quite a lot, just not AS relevant as this one) precedent to removing & editing old release notes:
e3812e1#diff-9538c800780031db3dfa7746f5a36fbbc895c60fdc896385bedd36940927427dL79-L81

@nazarewk

Copy link
Copy Markdown
Member Author

I have fixed the tests (I did some incompatible changes to the module on the way), but even though I've added option rename, the manual still doesn't build:
https://github.com/NixOS/nixpkgs/blob/c65c0c24c0b1fe683cea50f1c32893304cf0b1f7/nixos/modules/services/networking/netbird.nix#L75-L77

seems like there is some (actually quite a lot, just not AS relevant as this one) precedent to removing & editing old release notes: e3812e1#diff-9538c800780031db3dfa7746f5a36fbbc895c60fdc896385bedd36940927427dL79-L81

As per #287236 (comment) , I am temporarily using mkAliasOptionModule until there is a better implementation to build release notes without errors.

@fricklerhandwerk

Copy link
Copy Markdown
Contributor

Thanks a great deal for sticking through it, and sorry for the long delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants