Skip to content

Commit

Permalink
move systemd related code to separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nzbr committed Sep 11, 2023
1 parent d668d8e commit 3a68449
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 187 deletions.
2 changes: 1 addition & 1 deletion checks/shfmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
, ...
}:
runCommand "check-shfmt" { nativeBuildInputs = [ shfmt ]; } ''
shfmt -i 2 -d ${./../scripts}/*.sh
shfmt -i 2 -d $(find ${./..} -name '*.sh')
touch $out
''
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
./installer.nix
./interop.nix
./recovery.nix
./systemd
./version.nix
./wsl-conf.nix
./wsl-distro.nix
Expand Down
2 changes: 1 addition & 1 deletion modules/recovery.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ in
config = {
wsl.extraBin = [
# needs to be a copy, not a symlink, to be executable from outside
{ src = "${recovery}/bin/nixos-wsl-recovery"; copy = true;}
{ src = "${recovery}/bin/nixos-wsl-recovery"; copy = true; }
];
};

Expand Down
50 changes: 50 additions & 0 deletions modules/systemd/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ config, pkgs, lib, ... }:
with lib; {

imports = [
./native
./syschdemd
];

options.wsl = with types; {
nativeSystemd = mkOption {
type = bool;
default = false;
description = "Use native WSL systemd support";
};
};

config =
let
cfg = config.wsl;
in
mkIf (cfg.enable) {

wsl.binShPkg = if cfg.nativeSystemd then bashWrapper else pkgs.bashInteractive;

# systemd-oomd requires cgroup pressure info which WSL doesn't have
systemd.oomd.enable = false;

# useful for usbip but adds a dependency on various firmwares which are combined over 300 MB big
services.udev.enable = lib.mkDefault false;

systemd = {
# Disable systemd units that don't make sense on WSL
services = {
firewall.enable = false;
systemd-resolved.enable = lib.mkDefault false;
# systemd-timesyncd actually works in WSL and without it the clock can drift
systemd-timesyncd.unitConfig.ConditionVirtualization = "";
};

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

# Link the X11 socket into place. This is a no-op on a normal setup,
# but helps if /tmp is a tmpfs or mounted from some other location.
tmpfiles.rules = [ "L /tmp/.X11-unix - - - - ${cfg.wslConf.automount.root}/wslg/.X11-unix" ];
};

};

}
37 changes: 37 additions & 0 deletions modules/systemd/native/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, pkgs, lib, ... }:
with lib; {

config =
let
cfg = config.wsl;
nativeUtils = pkgs.callPackage ../scripts/native-utils { };
in
mkIf (cfg.enable && cfg.nativeSystemd) {
wsl.wslConf = {
user.default = config.users.users.${cfg.defaultUser}.name;
boot.systemd = true;
};

system.activationScripts = {
shimSystemd = stringAfter [ ] ''
echo "setting up /sbin/init shim..."
mkdir -p /sbin
ln -sf ${nativeUtils}/bin/systemd-shim /sbin/init
'';
setupLogin = lib.mkIf cfg.populateBin (stringAfter [ ] ''
echo "setting up /bin/login..."
mkdir -p /bin
ln -sf ${pkgs.shadow}/bin/login /bin/login
'');
};

environment = {
# preserve $PATH from parent
variables.PATH = [ "$PATH" ];
extraInit = ''
eval $(${nativeUtils}/bin/split-path --automount-root="${cfg.wslConf.automount.root}" ${lib.optionalString cfg.interop.includePath "--include-interop"})
'';
};
};

}
37 changes: 37 additions & 0 deletions modules/systemd/syschdemd/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, pkgs, lib, ... }:
with lib; {

options = { };

config =
let
cfg = config.wsl;

syschdemd = pkgs.callPackage ./syschdemd.nix {
automountPath = cfg.wslConf.automount.root;
defaultUser = config.users.users.${cfg.defaultUser};
};
in
mkIf (cfg.enable && !cfg.nativeSystemd) {

users.users.root.shell = "${syschdemd}/bin/syschdemd";
security.sudo.extraConfig = ''
Defaults env_keep+=INSIDE_NAMESPACE
'';
wsl.wslConf.user.default = "root";

# Start a systemd user session when starting a command through runuser
security.pam.services.runuser.startSession = true;

# Include Windows %PATH% in Linux $PATH.
environment.extraInit = mkIf cfg.interop.includePath ''PATH="$PATH:$WSLPATH"'';
environment.systemPackages = [
(pkgs.runCommand "wslpath" { } ''
mkdir -p $out/bin
ln -s /init $out/bin/wslpath
'')
];

};

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3a68449

Please sign in to comment.