From 14f29815df0b0957eb892cf13cb22c1e00558670 Mon Sep 17 00:00:00 2001 From: nzbr Date: Sat, 5 Oct 2024 01:42:26 +0200 Subject: [PATCH] shell-wrapper: wrap /bin/sh --- modules/systemd/native/wrap-shell.nix | 34 +++++++++++++++++---------- modules/wsl-distro.nix | 8 ++++++- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/modules/systemd/native/wrap-shell.nix b/modules/systemd/native/wrap-shell.nix index 6df6bea6..e5c65569 100644 --- a/modules/systemd/native/wrap-shell.nix +++ b/modules/systemd/native/wrap-shell.nix @@ -5,31 +5,39 @@ with lib; let cfg = config.wsl; + wrapShell = shellPath: + pkgs.stdenvNoCC.mkDerivation { + name = "wrapped-${last (splitString "/" (shellPath))}"; + buildCommand = '' + mkdir -p $out + cp ${config.system.build.nativeUtils}/bin/shell-wrapper $out/wrapper + ln -s ${shellPath} $out/shell + ''; + }; + users-groups-module = import "${modulesPath}/config/users-groups.nix" { inherit lib utils pkgs; config = recursiveUpdate config { users.users = mapAttrs (n: v: v // { - shell = - let - shellPath = utils.toShellPath v.shell; - wrapper = pkgs.stdenvNoCC.mkDerivation { - name = "wrapped-${last (splitString "/" (shellPath))}"; - buildCommand = '' - mkdir -p $out - cp ${config.system.build.nativeUtils}/bin/shell-wrapper $out/wrapper - ln -s ${shellPath} $out/shell - ''; - }; - in - wrapper.outPath + "/wrapper"; + shell = (wrapShell (utils.toShellPath v.shell)).outPath + "/wrapper"; }) config.users.users; }; }; in { + options.wsl.wrapBinSh = mkOption { + type = types.bool; + default = true; + description = '' + Wrap /bin/sh with a script that sets the correct environment variables (like the user shells). Only takes effect when using native systemd + ''; + }; + config = mkIf (cfg.enable && cfg.nativeSystemd) { system.activationScripts.users = users-groups-module.config.system.activationScripts.users; + + wsl.binShExe = mkIf config.wsl.wrapBinSh ((wrapShell "${config.wsl.binShPkg}/bin/sh").outPath + "/wrapper"); }; } diff --git a/modules/wsl-distro.nix b/modules/wsl-distro.nix index a357917c..78270fc6 100644 --- a/modules/wsl-distro.nix +++ b/modules/wsl-distro.nix @@ -15,6 +15,12 @@ in internal = true; description = "Package to be linked to /bin/sh. Mainly useful to be re-used by other modules like envfs."; }; + binShExe = mkOption { + type = str; + internal = true; + description = "Path to the shell executable to be linked to /bin/sh"; + default = "${config.wsl.binShPkg}/bin/sh"; + }; defaultUser = mkOption { type = str; default = "nixos"; @@ -212,7 +218,7 @@ in populateBin = true; extraBin = [ { src = "/init"; name = "wslpath"; } - { src = "${cfg.binShPkg}/bin/sh"; name = "sh"; } + { src = "${cfg.binShExe}"; name = "sh"; } { src = "${pkgs.util-linux}/bin/mount"; } ]; };