Skip to content

Commit

Permalink
shell-wrapper: wrap /bin/sh
Browse files Browse the repository at this point in the history
  • Loading branch information
nzbr committed Oct 5, 2024
1 parent acb21a7 commit 5a7c64a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
34 changes: 21 additions & 13 deletions modules/systemd/native/wrap-shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
}
8 changes: 7 additions & 1 deletion modules/wsl-distro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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"; }
];
};
Expand Down

0 comments on commit 5a7c64a

Please sign in to comment.