Skip to content

Commit

Permalink
wsl2: init a module to wrap systemd for WSL2
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh committed Feb 3, 2020
1 parent c94a5f3 commit 08d8c23
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions nixos/modules/virtualisation/wsl2.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ pkgs, config, lib, ... }:

{
boot.isContainer = true;

system.build.tarball = pkgs.callPackage ../../lib/make-system-tarball.nix {
compressCommand = "gzip";
compressionExtension = ".gz";
extraInputs = [ ];

contents = [ ];
extraArgs = "--owner=0";

storeContents = map (x: { object = x; symlink = "none"; }) [
config.system.build.toplevel
pkgs.stdenv
];
};

systemd.package = pkgs.wsl-systemd-wrapper;

services.nscd.enable = false;
networking.firewall.enable = false;
}
57 changes: 57 additions & 0 deletions pkgs/os-specific/windows/wsl-systemd-wrapper/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{ stdenv, writeShellScript, runtimeShell, procps, systemd, utillinux, lndir, daemonize }:

let
systemd-find =
writeShellScript "wsl-systemd-find" ''
${procps}/bin/pgrep -xf ${systemd}/lib/systemd/systemd || (echo "Can't find running systemd" >&2; exit 1)
'';
systemd-run =
writeShellScript "wsl-systemd-run" ''
exec ${utillinux}/bin/nsenter -t "$(${systemd-find})" -S $UID -m -p ${systemd}/bin/$(basename $0) $@
'';
in
stdenv.mkDerivation {
name = "wsl-systemd-wrapper";
dontUnpack = true;
nativeBuildInputs = [ lndir ];
installPhase = ''
mkdir $out
lndir ${systemd} $out
unlink $out/lib/systemd/systemd
cat > $out/lib/systemd/systemd <<EOF
#!${runtimeShell}
exec ${daemonize}/bin/daemonize ${utillinux}/bin/unshare -fp --propagation shared --mount-proc "${systemd}/lib/systemd/systemd" $@
EOF
chmod +x $out/lib/systemd/systemd
for bin in $out/bin/*; do
unlink $bin
ln -s ${systemd-run} $bin
done
unlink $out/bin/systemctl
cat > $out/bin/systemctl <<EOF
#!${runtimeShell}
if [ "\$1" = "daemon-reexec" ]; then
kill "\$(${systemd-find})"
while ${systemd-find} >/dev/null 2>/dev/null; do
echo "Waiting for systemd to exit"
sleep 1
done
$out/lib/systemd/systemd
echo -n "Waiting for systemd to start"
while ! systemctl is-system-running >/dev/null 2>/dev/null; do
echo -n "."
sleep 1
done
echo
dbus-send --system --print-reply --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.ListUnitsByPatterns array:string: array:string:
exit 0
fi
exec ${utillinux}/bin/nsenter -t "\$(${systemd-find})" -S \$UID -m -p ${systemd}/bin/systemctl \$@
EOF
chmod +x $out/bin/systemctl
'';
inherit (systemd) passthru;
}
4 changes: 4 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7324,6 +7324,10 @@ in

wrk2 = callPackage ../tools/networking/wrk2 { };

wsl-systemd-wrapper = callPackage ../os-specific/windows/wsl-systemd-wrapper {
inherit (xorg) lndir;
};

wuzz = callPackage ../tools/networking/wuzz { };

wv = callPackage ../tools/misc/wv { };
Expand Down

0 comments on commit 08d8c23

Please sign in to comment.