Skip to content

Commit

Permalink
Fix syschdemd exit code (#140)
Browse files Browse the repository at this point in the history
* use systemd-run instead of machinectl

* fix systemd user sessions by launching through runuser

* fix: add which to path
  • Loading branch information
nzbr committed Nov 13, 2022
1 parent a4bbcf3 commit e9e8d01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions modules/wsl-distro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ with lib; {
enableEmergencyMode = false;
};

# Start a systemd user session when starting a command through runuser
security.pam.services.runuser.startSession = true;

warnings = (optional (config.systemd.services.systemd-resolved.enable && config.wsl.wslConf.network.generateResolvConf)
"systemd-resolved is enabled, but resolv.conf is managed by WSL"
);
Expand Down
5 changes: 3 additions & 2 deletions scripts/syschdemd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, gnugrep
, systemd
, util-linux
, which
, defaultUser
, automountPath
,
Expand Down Expand Up @@ -44,10 +45,10 @@ mkWrappedScript {
glibc # getent
gnugrep
systemd # machinectl
util-linux # nsenter
util-linux # nsenter, runuser
which
wrapper
];
username = defaultUser.name;
uid = defaultUser.uid;
inherit automountPath;
}
14 changes: 9 additions & 5 deletions scripts/syschdemd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ main() {
shift
command="$*"
else
command=$(get_shell @username@)
command="$(get_shell @username@)"
fi

# If we're executed from inside the container, e.g. sudo
Expand All @@ -124,13 +124,17 @@ main() {
exportCmd="$(export -p | grep -vE ' (HOME|LOGNAME|SHELL|USER)=')"

run_in_namespace \
machinectl \
systemd-run \
--quiet \
--uid=@uid@ \
--collect \
--wait \
--pty \
--service-type=exec \
--setenv=INSIDE_NAMESPACE=true \
--setenv=WSLPATH="$(clean_wslpath)" \
shell .host \
/bin/sh -c "cd \"$PWD\"; $exportCmd; source /etc/set-environment; exec $command"
--working-directory="$PWD" \
--machine=.host \
"$(which runuser)" -u @username@ -- /bin/sh -c "$exportCmd; source /etc/set-environment; exec $command"
}

main "$@"

0 comments on commit e9e8d01

Please sign in to comment.