Skip to content

Commit 5c55c89

Browse files
committed
interop: enable preserveArgvZero
Fixes interop with latest WSL versions
1 parent 80c18db commit 5c55c89

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

modules/wsl-distro.nix

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,28 @@ with builtins; with lib;
6767
binfmt.registrations = mkIf cfg.interop.register {
6868
WSLInterop = {
6969
magicOrExtension = "MZ";
70-
interpreter = "/init";
70+
71+
# FIXME: temporary hack for https://github.com/nix-community/NixOS-WSL/pull/84
72+
#
73+
# WSL Preview 0.58 and up registers the /init binfmt interp for Windows executable
74+
# with the "preserve argv[0]" flag, so if you run `./foo.exe`, the interp gets invoked
75+
# as `/init foo.exe ./foo.exe`.
76+
# argv[0] --^ ^-- actual path
77+
#
78+
# Older versions expect to be called without the argv[0] bit, simply as `/init ./foo.exe`.
79+
#
80+
# We detect that by running `/init /known-not-existing-path.exe` and checking the exit code:
81+
# the new style interp expects at least two arguments, so exits with exit code 1,
82+
# presumably meaning "parsing error"; the old style interp attempts to actually run
83+
# the executable, fails to find it, and exits with 255.
84+
interpreter = pkgs.writeShellScript "nixos-wsl-binfmt-hack" ''
85+
/init /nixos-wsl-does-not-exist.exe
86+
[ $? -eq 255 ] && shift
87+
exec /init $@
88+
'';
89+
7190
fixBinary = true;
91+
preserveArgvZero = true;
7292
};
7393
};
7494
};

0 commit comments

Comments
 (0)