Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable direct installation with native systemd #243

Closed
wants to merge 1 commit into from

Conversation

htngr
Copy link

@htngr htngr commented Apr 12, 2023

With this fix wsl.nativeSystemd = true works in a fresh installation (should fix #156 (comment))

Minimal working configuration.nix:

wsl = {
  enable = true;
  nativeSystemd = true;
};

Note that wsl should be restarted after import (wsl --shutdown)

@nzbr
Copy link
Member

nzbr commented Apr 13, 2023

While this makes it possible to import a tarball where wsl.nativeSystemd is enabled, it will not work properly on the first start (it requires a manual restart of the distro for systemd to start). The better solution would be to extend the systemd shim so it is able to run the installer before handing over to systemd. And we shouldn't set a root shell. People want to be able to use different shells

@htngr
Copy link
Author

htngr commented Apr 13, 2023

Alright, seems reasonable. I've been trying to get the installer to work with systemd, but this seems to be only possible with some ugly hacks, because

  1. we can't switch between non-systemd WSL (boot.systemd = false in wsl.conf) and systemd-enabled WSL without restarting WSL. Therefore the installer needs to be systemd-enabled
  2. when starting the systemd-enabled installer (put the installer script in /sbin/init), WSL expects systemd to run at latest in 10 seconds. The extraction of the tarball, the activation and the cleaning of the nix store must be finished within this timeframe, or else WSL just kills the current process and moves on in the installer script. This process probably is tar xf, so the extraction stops and the rest of the script doesn't work.

The good news is, the installer is unneccessary if we add extraArgs = "--hard-dereference" to make-system-tarball. I've only added a script (which calls /nix/var/nix/profiles/system/activate and then systemd-shim) to /sbin/init which is only called on first start and is then overwritten by the activation scripts.

One problem which remains is that for the init script to work, user.default must be root (I just override this in the initial wsl.conf). But this means that after the install the user gets a root shell and even if user.default is reset to nixos by the NixOS activation, WSL must be restarted for the change to take effect.
Edit: works regardless of user.default

Another minor thing is that user@0.service fails, so systemctl status shows degraded. After systemctl restart user@0.service everything is working as expected:
Edit: this is likely another issue because it also happens after WSL restart and for other users as well (e.g. user@1000.service)

Apr 13 23:46:15 nixos systemd[1]: Starting User Manager for UID 0...
Apr 13 23:46:15 nixos systemd[1]: user@0.service: Main process exited, code=exited, status=219/CGROUP
Apr 13 23:46:15 nixos systemd[1]: user@0.service: Failed with result 'exit-code'.
Apr 13 23:46:15 nixos systemd[1]: Failed to start User Manager for UID 0.

@htngr htngr force-pushed the main branch 2 times, most recently from 670d8d7 to 8f0d947 Compare April 14, 2023 07:04
@htngr
Copy link
Author

htngr commented Apr 14, 2023

Also, would it make sense to add the workaround from microsoft/WSL#8879 (comment) to properly display the service hierarchy with systemctl status?

@K900
Copy link
Contributor

K900 commented Apr 14, 2023

I'd rather wait for this to be fixed upstream, we don't want to rebuild systemd.

@nzbr
Copy link
Member

nzbr commented Apr 14, 2023

Agreed, we shouldn't fix things that are broken on Microsoft's end. The goal is that NixOS behaves like any other WSL distro

@htngr htngr force-pushed the main branch 2 times, most recently from fd3a959 to 959cfdb Compare June 27, 2023 07:59
@htngr htngr changed the title Enable systemd in installer Enable direct installation with native systemd Jun 27, 2023
modules/systemd-tarball.nix Outdated Show resolved Hide resolved
modules/systemd-tarball.nix Outdated Show resolved Hide resolved
modules/systemd-tarball.nix Outdated Show resolved Hide resolved
@K900
Copy link
Contributor

K900 commented Jul 5, 2023

Do we even need an installer at all? Maybe we should just build everything into the tarball, and then have two tarballs, one for native systemd and one for syschdemd (and then drop syschdemd later).

@htngr
Copy link
Author

htngr commented Jul 5, 2023

Do we even need an installer at all? Maybe we should just build everything into the tarball, and then have two tarballs, one for native systemd and one for syschdemd (and then drop syschdemd later).

This PR removes the need for an installer with native systemd.

Some time ago I've also successfully installed NixOS-WSL (syschdemd, no installer) by setting extraArgs = "--hard-dereference"; in build-tarball.nix. But I don't know if this still works

@TeofilC
Copy link
Contributor

TeofilC commented Jul 5, 2023

FWIW I'm pretty sure "--hard-dereference" works. I've used it successfully in the past. Also I think the upstream bug in libarchive might have been fixed and the version updated on windows. My system has bsdtar 3.5.2 - libarchive 3.5.2 zlib/1.2.5.f-ipp bz2lib/1.0.6, which I think has the fix for libarchive/libarchive#1044

K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
K900 added a commit that referenced this pull request Sep 29, 2023
@K900
Copy link
Contributor

K900 commented Sep 29, 2023

Gonna close this in favor of #295 which removes the installer hackery entirely.

@K900 K900 closed this Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Systemd hangs on first boot
5 participants