Skip to content

Commit

Permalink
feat: use internal WSL knobs instead of symlinks
Browse files Browse the repository at this point in the history
This makes things so much easier and more consistent and it's undocumented API that I found in Ghidra so nothing could possibly go wrong.
  • Loading branch information
K900 committed Aug 27, 2023
1 parent faab319 commit 9a74386
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
15 changes: 15 additions & 0 deletions modules/wsl-conf.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ with lib; {
default = false;
description = "Use systemd as init. There's no need to enable this manually, use the wsl.nativeSystemd option instead";
};
initPath = mkOption {
type = path;
default = null;
internal = true;
};
initShutdownCommand = mkOption {
type = path;
default = null;
internal = true;
};
initWaitCommand = mkOption {
type = path;
default = null;
internal = true;
};
};
interop = {
enabled = mkOption {
Expand Down
23 changes: 11 additions & 12 deletions modules/wsl-distro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
with lib;

let
bashWrapper = pkgs.writeShellScriptBin "sh" ''
export PATH="$PATH:${lib.makeBinPath [ pkgs.systemd pkgs.gnugrep ]}"
exec ${pkgs.bashInteractive}/bin/sh "$@"
'';

nixos-enter' = config.system.build.nixos-enter.overrideAttrs (_: {
runtimeShell = "/bin/bash";
});
Expand Down Expand Up @@ -176,7 +171,7 @@ in

# require people to use lib.mkForce to make it harder to brick their installation
wsl = {
binShPkg = if cfg.nativeSystemd then bashWrapper else pkgs.bashInteractive;
binShPkg = pkgs.bashInteractive;
populateBin = true;
};

Expand Down Expand Up @@ -211,15 +206,19 @@ in
(mkIf cfg.nativeSystemd {
wsl.wslConf = {
user.default = config.users.users.${cfg.defaultUser}.name;
boot.systemd = true;
boot = {
systemd = true;
initPath = "${nativeUtils}/bin/systemd-shim";
initShutdownCommand = pkgs.writeShellScript "nixos-wsl-init-shutdown" ''
${pkgs.systemd}/bin/systemctl reboot
'';
initWaitCommand = pkgs.writeShellScript "nixos-wsl-init-wait" ''
${pkgs.systemd}/bin/systemctl is-system-running | ${pkgs.gnugrep}/bin/grep -E "(running|degraded)"
'';
};
};

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
Expand Down

0 comments on commit 9a74386

Please sign in to comment.