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

Refactor Code #291

Merged
merged 10 commits into from
Sep 12, 2023
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
result
result-*
scripts/*/target
nixos-wsl-installer.tar.gz
2 changes: 1 addition & 1 deletion checks/rustfmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
, ...
}:
runCommand "check-rustfmt" { nativeBuildInputs = [ cargo rustfmt ]; } ''
cargo fmt --manifest-path=${./../scripts/native-utils}/Cargo.toml --check
cargo fmt --manifest-path=${./../utils}/Cargo.toml --check
touch $out
''
2 changes: 1 addition & 1 deletion checks/shfmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
, ...
}:
runCommand "check-shfmt" { nativeBuildInputs = [ shfmt ]; } ''
shfmt -i 2 -d ${./../scripts}/*.sh
shfmt -i 2 -d $(find ${./..} -name '*.sh')
touch $out
''
15 changes: 4 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@

nixosModules.wsl = {
imports = [
./modules/build-tarball.nix
./modules/docker-desktop.nix
./modules/docker-native.nix
./modules/installer.nix
./modules/interop.nix
./modules/version.nix
./modules/wsl-conf.nix
./modules/wsl-distro.nix
./modules

({ ... }: {
wsl.version.rev = mkIf (self ? rev) self.rev;
Expand Down Expand Up @@ -62,11 +55,11 @@
};

packages = {
utils = pkgs.callPackage ./scripts/native-utils { };
staticUtils = pkgs.pkgsStatic.callPackage ./scripts/native-utils { };
utils = pkgs.callPackage ./utils { };
staticUtils = pkgs.pkgsStatic.callPackage ./utils { };
};

devShell = pkgs.mkShell {
devShells.default = pkgs.mkShell {
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

nativeBuildInputs = with pkgs; [
Expand Down
13 changes: 13 additions & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ ... }: {
imports = [
./build-tarball.nix
./docker
./installer.nix
./interop.nix
./recovery.nix
./systemd
./version.nix
./wsl-conf.nix
./wsl-distro.nix
];
}
6 changes: 6 additions & 0 deletions modules/docker/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ ... }: {
imports = [
./docker-desktop.nix
./native.nix
];
}
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions modules/recovery.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{ config, pkgs, lib, ... }:
with lib;
let

nixos-enter' = config.system.build.nixos-enter.overrideAttrs (_: {
runtimeShell = "/bin/bash";
});

recovery = pkgs.writeScriptBin "nixos-wsl-recovery" ''
#! /bin/sh
if [ -f /etc/NIXOS ]; then
echo "nixos-wsl-recovery should only be run from the WSL system distribution."
echo "Example:"
echo " wsl --system --distribution NixOS --user root -- /nix/var/nix/profiles/system/bin/nixos-wsl-recovery"
exit 1
fi
mount -o remount,rw /mnt/wslg/distro
exec /mnt/wslg/distro/${nixos-enter'}/bin/nixos-enter --root /mnt/wslg/distro "$@"
'';

in
{

config = {
wsl.extraBin = [
# needs to be a copy, not a symlink, to be executable from outside
{ src = "${recovery}/bin/nixos-wsl-recovery"; copy = true; }
];
};

}
48 changes: 48 additions & 0 deletions modules/systemd/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ config, pkgs, lib, ... }:
with lib; {

imports = [
./native
./syschdemd
];

options.wsl = with types; {
nativeSystemd = mkOption {
type = bool;
default = false;
description = "Use native WSL systemd support";
};
};

config =
let
cfg = config.wsl;
in
mkIf (cfg.enable) {

# systemd-oomd requires cgroup pressure info which WSL doesn't have
systemd.oomd.enable = false;

# useful for usbip but adds a dependency on various firmwares which are combined over 300 MB big
services.udev.enable = lib.mkDefault false;

systemd = {
# Disable systemd units that don't make sense on WSL
services = {
firewall.enable = false;
systemd-resolved.enable = lib.mkDefault false;
# systemd-timesyncd actually works in WSL and without it the clock can drift
systemd-timesyncd.unitConfig.ConditionVirtualization = "";
};

# Don't allow emergency mode, because we don't have a console.
enableEmergencyMode = false;

# Link the X11 socket into place. This is a no-op on a normal setup,
# but helps if /tmp is a tmpfs or mounted from some other location.
tmpfiles.rules = [ "L /tmp/.X11-unix - - - - ${cfg.wslConf.automount.root}/wslg/.X11-unix" ];
};

};

}
46 changes: 46 additions & 0 deletions modules/systemd/native/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{ config, pkgs, lib, ... }:
with lib; {

config =
let
cfg = config.wsl;
nativeUtils = pkgs.callPackage ../../../utils { };

bashWrapper = pkgs.writeShellScriptBin "sh" ''
export PATH="$PATH:${lib.makeBinPath [ pkgs.systemd pkgs.gnugrep ]}"
exec ${pkgs.bashInteractive}/bin/sh "$@"
'';
in
mkIf (cfg.enable && cfg.nativeSystemd) {

wsl = {
binShPkg = bashWrapper;
wslConf = {
user.default = config.users.users.${cfg.defaultUser}.name;
boot.systemd = true;
};
};

system.activationScripts = {
shimSystemd = stringAfter [ ] ''
echo "setting up /sbin/init shim..."
mkdir -p /sbin
ln -sf ${nativeUtils}/bin/systemd-shim /sbin/init
'';
setupLogin = lib.mkIf cfg.populateBin (stringAfter [ ] ''
echo "setting up /bin/login..."
mkdir -p /bin
ln -sf ${pkgs.shadow}/bin/login /bin/login
'');
};

environment = {
# preserve $PATH from parent
variables.PATH = [ "$PATH" ];
extraInit = ''
eval $(${nativeUtils}/bin/split-path --automount-root="${cfg.wslConf.automount.root}" ${lib.optionalString cfg.interop.includePath "--include-interop"})
'';
};
};

}
41 changes: 41 additions & 0 deletions modules/systemd/syschdemd/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ config, pkgs, lib, ... }:
with lib; {

options = { };

config =
let
cfg = config.wsl;

syschdemd = pkgs.callPackage ./syschdemd.nix {
automountPath = cfg.wslConf.automount.root;
defaultUser = config.users.users.${cfg.defaultUser};
};
in
mkIf (cfg.enable && !cfg.nativeSystemd) {

wsl = {
binShPkg = pkgs.bashInteractive;
wslConf.user.default = "root";
};

users.users.root.shell = "${syschdemd}/bin/syschdemd";
security.sudo.extraConfig = ''
Defaults env_keep+=INSIDE_NAMESPACE
'';

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

# Include Windows %PATH% in Linux $PATH.
environment.extraInit = mkIf cfg.interop.includePath ''PATH="$PATH:$WSLPATH"'';
environment.systemPackages = [
(pkgs.runCommand "wslpath" { } ''
mkdir -p $out/bin
ln -s /init $out/bin/wslpath
'')
];

};

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Loading