Skip to content

Commit

Permalink
Added systemd-tarball for direct install with native systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr authored and K900 committed Aug 26, 2023
1 parent faab319 commit 9300bb4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
./modules/docker-native.nix
./modules/installer.nix
./modules/interop.nix
./modules/systemd-tarball.nix
./modules/version.nix
./modules/wsl-conf.nix
./modules/wsl-distro.nix
Expand Down
82 changes: 82 additions & 0 deletions modules/systemd-tarball.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{ config, pkgs, lib, ... }:
with builtins; with lib;
let
pkgs2storeContents = map (x: { object = x; symlink = "none"; });

nixpkgs = lib.cleanSource pkgs.path;

channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}"
{ preferLocalBuild = true; }
''
mkdir -p $out
cp -prd ${nixpkgs.outPath} $out/nixos
chmod -R u+w $out/nixos
if [ ! -e $out/nixos/nixpkgs ]; then
ln -s . $out/nixos/nixpkgs
fi
echo -n ${toString config.system.nixos.revision} > $out/nixos/.git-revision
echo -n ${toString config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
echo ${toString config.system.nixos.versionSuffix} | sed -e s/pre// > $out/nixos/svn-revision
'';

preparer = pkgs.writeShellScriptBin "wsl-prepare" ''
set -e
# Set system profile
system=${config.system.build.toplevel}
./$system/sw/bin/nix-store --store "$PWD" --load-db < ./nix-path-registration
rm ./nix-path-registration
./$system/sw/bin/nix-env --store "$PWD" -p ./nix/var/nix/profiles/system --set $system
# Set channel
mkdir -p ./nix/var/nix/profiles/per-user/root
./$system/sw/bin/nix-env --store "$PWD" -p ./nix/var/nix/profiles/per-user/root/channels --set ${channelSources}
mkdir -m 0700 -p ./root/.nix-defexpr
ln -s /nix/var/nix/profiles/per-user/root/channels ./root/.nix-defexpr/channels
mkdir -p sbin nix/nixos-wsl/entrypoint
ln -s ${pkgs.wslNativeUtils}/bin/systemd-shim ./sbin/init
${lib.optionalString config.wsl.tarball.includeConfig ''
# Copy the system configuration
mkdir -p ./etc/nixos/nixos-wsl
cp -R ${lib.cleanSource ../.}/. ./etc/nixos/nixos-wsl
mv ./etc/nixos/nixos-wsl/configuration.nix ./etc/nixos/configuration.nix
# Patch the import path to avoid having a flake.nix in /etc/nixos
sed -i 's|import \./default\.nix|import \./nixos-wsl|' ./etc/nixos/configuration.nix
''}
'';
in
{

config = mkIf config.wsl.enable {
assertions = [{
assertion = config.wsl.nativeSystemd;
message = "config.wsl.nativeSystemd must be true in order to build a systemd-tarball!";
}];

system.build.systemd-tarball = pkgs.callPackage "${nixpkgs}/nixos/lib/make-system-tarball.nix" {

contents = [
# WSL needs this before first activation
{ inherit (config.environment.etc."wsl.conf") source; target = "/etc/wsl.conf"; }
];

fileName = "nixos-wsl-${pkgs.hostPlatform.system}";

storeContents = pkgs2storeContents [
config.system.build.toplevel
channelSources
preparer
];

extraCommands = "${preparer}/bin/wsl-prepare";
extraArgs = "--hard-dereference";

# Use gzip
compressCommand = "gzip";
compressionExtension = ".gz";
};

};
}
12 changes: 8 additions & 4 deletions modules/wsl-distro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ in
automountPath = cfg.wslConf.automount.root;
defaultUser = config.users.users.${cfg.defaultUser};
};

nativeUtils = pkgs.callPackage ../scripts/native-utils { };
in
mkIf cfg.enable (
mkMerge [
Expand Down Expand Up @@ -98,6 +96,12 @@ in
];
};

nixpkgs.overlays = [
(_: prev: {
wslNativeUtils = prev.callPackage ../scripts/native-utils { };
})
];

# dhcp is handled by windows
networking.dhcpcd.enable = false;

Expand Down Expand Up @@ -218,7 +222,7 @@ in
shimSystemd = stringAfter [ ] ''
echo "setting up /sbin/init shim..."
mkdir -p /sbin
ln -sf ${nativeUtils}/bin/systemd-shim /sbin/init
ln -sf ${pkgs.wslNativeUtils}/bin/systemd-shim /sbin/init
'';
setupLogin = lib.mkIf cfg.populateBin (stringAfter [ ] ''
echo "setting up /bin/login..."
Expand All @@ -231,7 +235,7 @@ in
# 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"})
eval $(${pkgs.wslNativeUtils}/bin/split-path --automount-root="${cfg.wslConf.automount.root}" ${lib.optionalString cfg.interop.includePath "--include-interop"})
'';
};
})
Expand Down

0 comments on commit 9300bb4

Please sign in to comment.