Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell-wrapper: wrap /bin/sh #561

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading