Skip to content

Allow gc-ing with a rootless daemon#5380

Open
thufschmitt wants to merge 23 commits intomasterfrom
rootless-daemon
Open

Allow gc-ing with a rootless daemon#5380
thufschmitt wants to merge 23 commits intomasterfrom
rootless-daemon

Conversation

@thufschmitt
Copy link
Member

@thufschmitt thufschmitt commented Oct 13, 2021

Depends on #5226

This PR adds the possibility to defer the collection of the gc roots to an external daemon (communicating with it through a UNIX socket).

The motivation for that is that (excluding #5226), finding the GC roots is the only part of Nix that strictly requires root. So deferring that part to an external tool means that it’s possible to run the Nix daemon as a non-root user (with the obvious security gains that come with it).

Along with it is included such an external tracing deamon (under src/nix-find-roots), written in such a way that it doesn’t depend on anything but the c++ stl.

https://github.com/regnat/rootless-nix-daemon-test provides a sample setup in which the nix-daemon is running as a normal nix-daemon user.

Using this feature is currently guarded behind the external-gc-daemon experimental flag

Fix #5208

@thufschmitt thufschmitt added the feature Feature request or proposal label Oct 13, 2021
@thufschmitt thufschmitt force-pushed the rootless-daemon branch 2 times, most recently from ffe4be5 to 26355d3 Compare October 13, 2021 11:16
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/tweag-nix-dev-updates-19/15488/1

@thufschmitt thufschmitt marked this pull request as ready for review November 15, 2021 17:43
@thufschmitt thufschmitt force-pushed the rootless-daemon branch 2 times, most recently from 2fceae5 to 6345401 Compare April 13, 2022 09:31
@fricklerhandwerk
Copy link
Contributor

Discussed in Nix team meeting on 2022-12-19:

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2022-12-16-nix-team-meeting-minutes-17/24120/1

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-01-13-nix-team-meeting-minutes-23/24644/1

@dpulls
Copy link

dpulls bot commented Jan 30, 2023

🎉 All dependencies have been resolved !

@thufschmitt thufschmitt requested a review from edolstra as a code owner June 14, 2023 10:01
@github-actions github-actions bot added store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority labels Jun 14, 2023
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/tweag-nix-dev-update-50/29793/1

@github-actions github-actions bot removed the with-tests Issues related to testing. PRs with tests have some priority label Oct 17, 2023
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Oct 24, 2023
Théophane Hufschmitt and others added 12 commits February 23, 2024 10:00
Not really needed since it's configurable from the config (and people
can always use `$NIX_CONFIG` if they really need to configure it from
the CLI)
Based on an offline review by @mopleen (thanks!)
It is no longer needed. See f4a8426
which did the same thing in the rest of Nix.
The test plan is taken from
https://github.com/thufschmitt/rootless-nix-daemon-test. That
intentionally used non-NixOS to get around the ambient Nix daemon, but
with newer NixOS we can in fact disable the ambient Nix daemon an run
our own!

A few things which are needed to make this nicer in the future

- NixOS/nixpkgs#3702

  A now-fixed issue, but won't be available until 23.05

- NixOS/nixpkgs#263248
  NixOS/nixpkgs#263250

  Newly opened issues inspired by the process of writing this test.
I misunderstand what was going on and NixOS/nixpkgs#263248 is a non-issue. That means we can improve the code right away.

Thank you @eclairevoyant for tipping me off that I was mistaken!
Otherwise the tests get stuck when using the daemon (I didn't understand
why though, so this is a dirty patch more than a proper fix, but at
least it works now)
Move its definition before a loop that might change `$!`
@Ericson2314
Copy link
Member

Ericson2314 commented Feb 23, 2024

Are we just waiting for a final review after re-agreeing on the "don't use other libraries approach"? We did say we wanted to do this after releasing 2.20.

Oh there was the matter of docs from @roberth too.

Théophane Hufschmitt and others added 5 commits February 27, 2024 06:37
Not supported by the installer because there are many moving parts, but
a rough installation guide can help be used for people to get a custom
installation for their needs
That would defeat the whole purpose of the thing as it would provide a
nice escalation path from a Nix vulnerability to root access
We got rid of them in the rest of the code, so let's do the same here.
Has been made a bit more lenient upstream, so match that
Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Have reviewed the docs.

@@ -0,0 +1,138 @@
# Using Nix in multi-user mode with a non-root daemon

> Experimental blurb
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a placeholder. Was this WIP?

sudo systemctl start nix-daemon.socket
sudo systemctl start nix-find-roots.socket
```
10. Profit
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
10. Profit
The daemons are now set up. You may try it:
```sh
head -c 20 /dev/urandom >test.data
nix store add-file test.data
nix store gc --dry-run
```

To my surprise nix store gc --dry-run seems to be real.
It is horribly slow on my store though, so I've interrupted it.

WantedBy=sockets.target
EOF
```
7. Install the systemd services for the tracing daemon:
Copy link
Member

Choose a reason for hiding this comment

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

Is it a "gc roots" or "tracing" daemon?
We should use one name everywhere.

Comment on lines +17 to +18
# No root daemon
nix.enable = false;
Copy link
Member

Choose a reason for hiding this comment

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

non-blocking:

Exposing a module for this kind of setup would be nice, but needs a few improvements.

I think you could blacklist the daemon module, at least on recent enough NixOS the daemon-specific stuff is separate from the Nix installation and config file.

I think we need a nix.daemon.enable in NixOS to be sure.

@twoolie
Copy link

twoolie commented Sep 11, 2024

Is there anything still blocking progress on this PR?

GHSA-h4vv-h3jq-v493 has shown that getting to a rootless daemon is still important.

@tomberek
Copy link
Contributor

Is there anything still blocking progress on this PR?

GHSA-h4vv-h3jq-v493 has shown that getting to a rootless daemon is still important.

Needs a rebase, some attention, and someone to shepherd it forward. I'm not aware of any fundamental blockers.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-10-02-nix-team-meeting-minutes-183/53815/1


systemd.user.sockets.nix-daemon = {
};
systemd.user.services.nix-daemon = {
Copy link
Member

Choose a reason for hiding this comment

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

You probably don't want user services, those will only start on a user's first login session. This should be a system service running as the user nix-daemon

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

Labels

feature Feature request or proposal store Issues and pull requests concerning the Nix store tests with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make it possible to run the Nix daemon as a non-root user

10 participants