diff options
| author | nzbr <mail@nzbr.de> | 2022-02-03 16:25:52 +0100 |
|---|---|---|
| committer | nzbr <mail@nzbr.de> | 2022-02-08 12:24:30 +0100 |
| commit | 5b81e1e59e127c24778cb7351bdb8e49fe0c3dec (patch) | |
| tree | 5e7d44c8d189f9681e87a621f7b80b6255cb9e10 | |
| parent | e844679c6a0413dae78476268e191f0b4844c164 (diff) | |
create an installer tarball to circumvent the unspecified error when running wsl --import
| -rw-r--r-- | .github/workflows/main.yml | 35 | ||||
| -rw-r--r-- | configuration.nix | 11 | ||||
| -rw-r--r-- | flake.nix | 11 | ||||
| -rw-r--r-- | modules/build-tarball.nix | 7 | ||||
| -rw-r--r-- | modules/installer.nix | 88 | ||||
| -rw-r--r-- | modules/wsl-distro.nix | 1 |
6 files changed, 135 insertions, 18 deletions
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 600cbfc..f5e552c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,11 +28,22 @@ jobs: run: | nix build '.#nixosConfigurations.mysystem.config.system.build.tarball' - - uses: actions/upload-artifact@v2 + - name: Upload tarball + uses: actions/upload-artifact@v2 with: - name: install.tar.gz + name: rootfs path: result/tarball/nixos-wsl-x86_64-linux.tar.gz + - name: Build installer + run: | + nix build '.#nixosConfigurations.mysystem.config.system.build.installer' + + - name: Upload installer + uses: actions/upload-artifact@v2 + with: + name: installer + path: result/tarball/nixos-wsl-installer.tar.gz + release: if: startsWith(github.ref, 'refs/tags/') needs: build @@ -43,7 +54,11 @@ jobs: - uses: actions/download-artifact@v2 with: - name: install.tar.gz + name: rootfs + + - uses: actions/download-artifact@v2 + with: + name: installer - name: Create Release id: create_release @@ -59,7 +74,17 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - upload_url: ${{ steps.create_release.outputs.upload_url }} # - asset_name: install.tar.gz + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_name: nixos-wsl-x86_64-linux.tar.gz asset_path: result/tarball/nixos-wsl-x86_64-linux.tar.gz asset_content_type: application/gzip + + - name: Upload installer as release asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_name: nixos-wsl-installer.tar.gz + asset_path: result/tarball/nixos-wsl-installer.tar.gz + asset_content_type: application/gzip diff --git a/configuration.nix b/configuration.nix index 7fe5c53..89b700b 100644 --- a/configuration.nix +++ b/configuration.nix @@ -2,18 +2,14 @@ with lib; let - defaultUser = "nixos"; - automountPath = "/mnt"; syschdemd = import ./syschdemd.nix { inherit lib pkgs config defaultUser; }; nixos-wsl = import ./default.nix; in { - imports = with nixos-wsl.nixosModules; [ + imports = [ "${modulesPath}/profiles/minimal.nix" - build-tarball - docker-desktop - wsl-distro + nixos-wsl.nixosModules.wsl ]; wsl = { @@ -26,7 +22,8 @@ in # docker.enable = true; }; - # Enable nix-flakes by default + # Enable nix flakes + nix.package = pkgs.nixFlakes; nix.extraOptions = '' experimental-features = nix-command flakes ''; @@ -13,10 +13,13 @@ outputs = { self, nixpkgs, flake-utils, ... }: { - nixosModules = { - build-tarball = import ./modules/build-tarball.nix; - wsl-distro = import ./modules/wsl-distro.nix; - docker-desktop = import ./modules/docker-desktop.nix; + nixosModules.wsl = { + imports = [ + ./modules/build-tarball.nix + ./modules/wsl-distro.nix + ./modules/docker-desktop.nix + ./modules/installer.nix + ]; }; nixosConfigurations.mysystem = nixpkgs.lib.nixosSystem { diff --git a/modules/build-tarball.nix b/modules/build-tarball.nix index cd90e6c..03d6a60 100644 --- a/modules/build-tarball.nix +++ b/modules/build-tarball.nix @@ -50,8 +50,11 @@ let cp ${config.environment.etc."wsl.conf".source} ./etc/wsl.conf # Copy the system configuration - mkdir -p ./etc/nixos - cp -R ${../.}/. ./etc/nixos/ + 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 havin a flake.nix in /etc/nixos + sed -i 's|import \./default\.nix|import \./nixos-wsl|' ./etc/nixos/configuration.nix ''; in diff --git a/modules/installer.nix b/modules/installer.nix new file mode 100644 index 0000000..62a750d --- /dev/null +++ b/modules/installer.nix @@ -0,0 +1,88 @@ +{ config, lib, pkgs, ... }: +with builtins; with lib; { + + config = mkIf config.wsl.enable ( + let + mkTarball = pkgs.callPackage "${lib.cleanSource pkgs.path}/nixos/lib/make-system-tarball.nix"; + + busybox-static = pkgs.busybox.override { enableStatic = true; }; + + pkgs2storeContents = map (x: { object = x; symlink = "none"; }); + + installerDependencies = mkTarball { + fileName = "store"; + compressCommand = "cat"; + compressionExtension = ""; + contents = [ ]; + storeContents = with pkgs; pkgs2storeContents [ + pv + ]; + }; + + installer = pkgs.writeScript "installer.sh" '' + #!/bin/sh + set -e + cd / + + echo "Unpacking installer..." + tar xf /store.tar + + echo "Unpacking root file system..." + ${pkgs.pv}/bin/pv /rootfs.tar.gz | tar xz + + echo "Cleaning up installer files..." + rm /rootfs.tar.gz /store.tar /installer.sh + + echo "Activating nix configuration..." + /nix/var/nix/profiles/system/activate + + echo "Starting systemd..." + exec ${config.users.users.root.shell} + ''; + + # Set installer.sh as the root shell + passwd = pkgs.writeText "passwd" '' + root:x:0:0:System administrator:/root:/installer.sh + ''; + in + { + + system.build.installer = mkTarball { + fileName = "nixos-wsl-installer"; + compressCommand = "gzip"; + compressionExtension = ".gz"; + + contents = [ + { + source = let tarball = config.system.build.tarball; in "${tarball}/tarball/${tarball.fileName}.tar${tarball.extension}"; + target = "/rootfs.tar.gz"; + } + { source = "${installerDependencies}/tarball/store.tar"; target = "/store.tar"; } + { source = "${busybox-static}/bin/busybox"; target = "/bin/busybox"; } + { source = config.environment.etc."wsl.conf".source; target = "/etc/wsl.conf"; } + { source = passwd; target = "/etc/passwd"; } + { source = installer; target = "/installer.sh"; } + ]; + + extraCommands = + let + # WSL's --import does not like hardlinks, create symlinks instead + createBusyboxLinks = concatStringsSep "\n" ( + mapAttrsToList + (applet: type: "ln -s /bin/busybox ./bin/${applet}") + (filterAttrs + (applet: type: applet != "busybox") # don't overwrite the original busybox + (readDir "${busybox-static}/bin") + ) + ); + in + pkgs.writeShellScript "busybox-setup" '' + set -e + ${createBusyboxLinks} + ''; + }; + + } + ); + +} diff --git a/modules/wsl-distro.nix b/modules/wsl-distro.nix index fce74ee..c2eb550 100644 --- a/modules/wsl-distro.nix +++ b/modules/wsl-distro.nix @@ -48,6 +48,7 @@ with builtins; with lib; # WSL is closer to a container than anything else boot.isContainer = true; + environment.noXlibs = lib.mkForce false; # override xlibs not being installed (due to isContainer) to enable the use of GUI apps environment = { # Include Windows %PATH% in Linux $PATH. |
