Systemd user service not started

I’m trying out nixos currently using the WSL2 nixos distro at https://github.com/Trundle/NixOS-WSL

It seems to be working mostly flawlessly, however I appear to not have the systemd user service:

nixos@nixos ~ [1]> ps aux | grep systemd
root         1  0.0  0.0  25456 11096 ?        Ss   14:20   0:00 systemd
root        21  0.0  0.0  36996 12568 ?        Ss   14:20   0:00 /nix/store/q6ylicsava582g55mdx0788yg2fib7js-systemd-246.6/lib/systemd/systemd-journald
root        51  0.0  0.0  16568  5776 ?        Ss   14:20   0:00 /nix/store/q6ylicsava582g55mdx0788yg2fib7js-systemd-246.6/lib/systemd/systemd-logind
message+    53  0.0  0.0   6368  3680 ?        Ss   14:20   0:00 /nix/store/4013xmv3pjddrdgb6vkncb4iqlyij8pq-dbus-1.12.20/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
nixos    16197  0.0  0.0 223440  2364 pts/0    S+   18:47   0:00 /nix/store/3mx947xcmsyk79izqc8ifv31qafp80pc-gnugrep-3.6/bin/grep --color=auto systemd

This means that whenever I (or specifically lorri) try to interact with it, the command will fail:

nixos@nixos ~> systemctl status --user
Failed to connect to bus: No such file or directory

afaik I need somehow to start the user systemdm process. According to the Arch wiki this is meant to be done by pam, however I gather that in nixos pam is difficult and normally managed by a login/display manager (which isn’t in the WSL version…).

Relevant files:

nixos@nixos ~ [1]> cat /etc/nixos/configuration.nix
{ lib, pkgs, config, modulesPath, ... }:

with lib;
let
  defaultUser = "nixos";
  syschdemd = import ./syschdemd.nix { inherit lib pkgs config defaultUser; };
in
{
  imports = [
    "${modulesPath}/profiles/minimal.nix"
  ];

  # WSL is closer to a container than anything else
  boot.isContainer = true;

  environment.etc.hosts.enable = false;
  environment.etc."resolv.conf".enable = false;

  networking.dhcpcd.enable = false;

  users.users.${defaultUser} = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
  };

  users.users.root = {
    shell = "${syschdemd}/bin/syschdemd";
    # Otherwise WSL fails to login as root with "initgroups failed 5"
    extraGroups = [ "root" ];
  };

  security.sudo.wheelNeedsPassword = false;

  # Disable systemd units that don't make sense on WSL
  systemd.services."serial-getty@ttyS0".enable = false;
  systemd.services."serial-getty@hvc0".enable = false;
  systemd.services."getty@tty1".enable = false;
  systemd.services."autovt@".enable = false;

  systemd.services.firewall.enable = false;
  systemd.services.systemd-resolved.enable = false;
  systemd.services.systemd-udevd.enable = false;

  # Don't allow emergency mode, because we don't have a console.
  systemd.enableEmergencyMode = false;

  # Enable DBUS
  services.dbus.enable = true;

  nix.extraOptions = ''
    keep-outputs = true
    keep-derivations = true
  '';
1 Like

WSL doesn’t support systemd properly.

Systemd strictly requires to be started as PID 1, and WSL starts it’s own process manager there to be able to do it’s magic.

The repo I linked to appears to have hacked around this by starting systemd in its own process id space, which they are claiming should fix the problem.

I was thinking that this was in the NixOS layer since it is a user level problem of systemd --user not starting (presumably because there isn’t a proper login manager).

However I most certainly can be wrong about this.

(I’ve opened an issue on that repo to look into this as well)