2023-06-18 00:46:31 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
with builtins;
|
|
|
|
with lib; {
|
2022-08-30 14:29:44 +00:00
|
|
|
config = mkIf config.wsl.enable (
|
|
|
|
let
|
|
|
|
mkTarball = pkgs.callPackage "${lib.cleanSource pkgs.path}/nixos/lib/make-system-tarball.nix";
|
|
|
|
|
2023-06-18 00:46:31 +00:00
|
|
|
pkgs2storeContents = map (x: {
|
|
|
|
object = x;
|
|
|
|
symlink = "none";
|
|
|
|
});
|
2022-08-30 14:29:44 +00:00
|
|
|
|
|
|
|
rootfs = let tarball = config.system.build.tarball; in "${tarball}/tarball/${tarball.fileName}.tar${tarball.extension}";
|
|
|
|
|
|
|
|
installer = pkgs.writeScript "installer.sh" ''
|
|
|
|
#!${pkgs.busybox}/bin/sh
|
|
|
|
BASEPATH=$PATH
|
|
|
|
export PATH=$BASEPATH:${pkgs.busybox}/bin # Add busybox to path
|
|
|
|
|
|
|
|
set -e
|
|
|
|
cd /
|
|
|
|
|
|
|
|
echo "Unpacking root file system..."
|
|
|
|
${pkgs.pv}/bin/pv ${rootfs} | tar xz
|
|
|
|
|
|
|
|
echo "Activating nix configuration..."
|
|
|
|
/nix/var/nix/profiles/system/activate
|
|
|
|
PATH=$BASEPATH:/run/current-system/sw/bin # Use packages from target system
|
|
|
|
|
|
|
|
echo "Cleaning up installer files..."
|
|
|
|
nix-collect-garbage
|
|
|
|
rm /nix-path-registration
|
|
|
|
|
|
|
|
echo "Optimizing store..."
|
|
|
|
nix-store --optimize
|
|
|
|
|
|
|
|
# Don't package the shell here, it's contained in the rootfs
|
|
|
|
exec ${builtins.unsafeDiscardStringContext config.users.users.root.shell} "$@"
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Set installer.sh as the root shell
|
|
|
|
passwd = pkgs.writeText "passwd" ''
|
|
|
|
root:x:0:0:System administrator:/root:${installer}
|
|
|
|
'';
|
2023-06-18 00:46:31 +00:00
|
|
|
in {
|
2022-08-30 14:29:44 +00:00
|
|
|
system.build.installer = mkTarball {
|
|
|
|
fileName = "nixos-wsl-installer";
|
|
|
|
compressCommand = "gzip";
|
|
|
|
compressionExtension = ".gz";
|
|
|
|
extraArgs = "--hard-dereference";
|
|
|
|
|
2023-06-18 00:46:31 +00:00
|
|
|
storeContents = with pkgs;
|
|
|
|
pkgs2storeContents [
|
|
|
|
installer
|
|
|
|
];
|
2022-08-30 14:29:44 +00:00
|
|
|
|
|
|
|
contents = [
|
2023-06-18 00:46:31 +00:00
|
|
|
{
|
|
|
|
source = config.environment.etc."wsl.conf".source;
|
|
|
|
target = "/etc/wsl.conf";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source = config.environment.etc."fstab".source;
|
|
|
|
target = "/etc/fstab";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source = passwd;
|
|
|
|
target = "/etc/passwd";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source = "${pkgs.busybox}/bin/busybox";
|
|
|
|
target = "/bin/sh";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source = "${pkgs.busybox}/bin/busybox";
|
|
|
|
target = "/bin/mount";
|
|
|
|
}
|
2022-08-30 14:29:44 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
extraCommands = pkgs.writeShellScript "prepare" ''
|
|
|
|
export PATH=$PATH:${pkgs.coreutils}/bin
|
|
|
|
mkdir -p bin
|
|
|
|
ln -s /init bin/wslpath
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|