blob: d2e523127baebbc3ae12d00ba1336828ced55bd2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
{ config, pkgs, lib, ... }:
with builtins; with lib;
let
cfg = config.wsl.tarball;
pkgs2storeContents = l: map (x: { object = x; symlink = "none"; }) l;
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
mkdir -m 0755 ./bin ./etc
mkdir -m 1777 ./tmp
# WSL requires a /bin/sh - only temporary, NixOS's activate will overwrite
ln -s ${config.users.users.root.shell} ./bin/sh
# WSL also requires a /bin/mount, otherwise the host fs isn't accessible
ln -s /nix/var/nix/profiles/system/sw/bin/mount ./bin/mount
# 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
# It's now a NixOS!
touch ./etc/NIXOS
# Write wsl.conf so that it is present when NixOS is started for the first time
cp ${config.environment.etc."wsl.conf".source} ./etc/wsl.conf
'' + lib.optionalString cfg.includeConfig ''
${if cfg.configPath == null then ''
# 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
'' else ''
mkdir -p ./etc/nixos
cp -R ${lib.cleanSource cfg.configPath}/. ./etc/nixos
''}
chmod -R u+w etc/nixos
'');
in
{
options.wsl.tarball = {
includeConfig = mkOption {
type = types.bool;
default = true;
description = "Whether or not to copy the system configuration into the tarball";
};
configPath = mkOption {
type = types.nullOr types.path;
default = null;
description = "Path to system configuration which is copied into the tarball";
};
};
config = mkIf config.wsl.enable {
# These options make no sense without the wsl-distro module anyway
system.build.tarball = pkgs.callPackage "${nixpkgs}/nixos/lib/make-system-tarball.nix" {
contents = [
{ source = config.users.users.root.shell; target = "/nix/nixos-wsl/entrypoint"; }
];
fileName = "nixos-wsl-${pkgs.hostPlatform.system}";
storeContents = pkgs2storeContents [
config.system.build.toplevel
channelSources
preparer
];
extraCommands = "${preparer}/bin/wsl-prepare";
# Use gzip
compressCommand = "gzip";
compressionExtension = ".gz";
};
};
}
|