Skip to content

Commit

Permalink
feat: rewrite tarball generation to use proper nixos-install + nixos-…
Browse files Browse the repository at this point in the history
…enter

Supersedes #243.
  • Loading branch information
K900 committed Sep 29, 2023
1 parent 65ba7e6 commit c92a7ee
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 203 deletions.
24 changes: 17 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,25 @@ jobs:
echo ${{ needs.prepare.outputs.version }} > ./VERSION
echo $(git rev-parse HEAD) >> ./VERSION
- name: Build installer 🛠️
- name: Build tarballs 🛠️
# We can't just nix run here because nix is not on root's PATH in the container
run: |
nix build '.#nixosConfigurations.mysystem.config.system.build.installer'
nix build .#nixosConfigurations.modern.config.system.build.tarballBuilder
sudo ./result nixos-wsl.tar.gz
nix build .#nixosConfigurations.legacy.config.system.build.tarballBuilder
sudo ./result nixos-wsl-legacy.tar.gz
- name: Upload installer 📤
- name: Upload tarball 📤
uses: actions/upload-artifact@v3
with:
name: installer
path: result/tarball/nixos-wsl-installer.tar.gz
name: tarball
path: nixos-wsl.tar.gz

- name: Upload legacy tarball 📤
uses: actions/upload-artifact@v3
with:
name: tarball-legacy
path: nixos-wsl-legacy.tar.gz

checks:
name: Flake Check 📋
Expand Down Expand Up @@ -117,10 +127,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Download installer 📥
- name: Download tarball 📥
uses: actions/download-artifact@v3
with:
name: installer
name: tarball-legacy

- name: Execute test 🧪
shell: pwsh
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ jobs:
name: Create Release 📢
runs-on: ubuntu-latest
steps:
- name: Download installer 📥
- name: Download tarball 📥
uses: actions/download-artifact@v3
with:
name: installer

- name: Generate checksums 🔑
run: |
mv */*.tar.gz .
for x in *.tar.gz; do
sha256sum $x > ${x}.sha256
done
Expand All @@ -29,7 +28,9 @@ jobs:
uses: softprops/action-gh-release@v1
with:
files: |
nixos-wsl-installer.tar.gz
nixos-wsl-installer.tar.gz.sha256
nixos-wsl.tar.gz
nixos-wsl.tar.gz.sha256
nixos-wsl-legacy.tar.gz
nixos-wsl-legacy.tar.gz.sha256
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
result
result-*
nixos-wsl-installer.tar.gz
nixos-wsl*.tar.gz
61 changes: 19 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ A minimal root filesystem for running NixOS on WSL. It can be used with
[DistroLauncher](https://github.com/microsoft/WSL-DistroLauncher) as
`install.tar.gz` or as input to `wsl --import --version 2`.

## System requirements

NixOS-WSL is tested with the Windows Store version of WSL 2, which is now available on all supported Windows releases (both 10 and 11).
Support for older "inbox" versions is best-effort.

## Quick start

First, [download the latest release\'s installer](https://github.com/nix-community/NixOS-WSL/releases/latest).
First, [download the latest release](https://github.com/nix-community/NixOS-WSL/releases/latest).

Then open up a Terminal, PowerShell or Command Prompt and run:

```sh
wsl --import NixOS .\NixOS\ nixos-wsl-installer.tar.gz --version 2
wsl --import NixOS .\NixOS\ nixos-wsl.tar.gz --version 2
```

This sets up a new WSL distribution `NixOS` that is installed under
`.\NixOS`. `nixos-wsl-installer.tar.gz` is the path to the file you
`.\NixOS`. `nixos-wsl.tar.gz` is the path to the file you
downloaded earlier. You might need to change this path or change to the download directory first.

You can now run NixOS:
Expand All @@ -29,11 +34,6 @@ You can now run NixOS:
wsl -d NixOS
```

The installer will unpack the file system and subsequently start NixOS.
A few warnings about file systems and locales will pop up. You can
safely ignore them. After systemd has started, you should be greeted
with a bash prompt inside your fresh NixOS installation.

If you want to make NixOS your default distribution, you can do so with

```sh
Expand Down Expand Up @@ -68,57 +68,34 @@ If you have a flakes-enabled Nix, you can use the following command to
build your own tarball instead of relying on a prebuilt one:

```cmd
nix build github:nix-community/NixOS-WSL#nixosConfigurations.mysystem.config.system.build.installer
sudo nix run github:nix-community/NixOS-WSL#nixosConfigurations.modern.config.system.build.tarballBuilder
```

Or, if you want to build with local changes, run inside your checkout:

```cmd
nix build .#nixosConfigurations.mysystem.config.system.build.installer
sudo nix run .#nixosConfigurations.your-hostname.config.system.build.tarballBuilder
```

Without a flakes-enabled Nix, you can build a tarball using:

```cmd
nix-build -A nixosConfigurations.mysystem.config.system.build.installer
```
nix-build -A nixosConfigurations.mysystem.config.system.build.tarballBuilder && sudo ./result/bin/nixos-wsl-tarball-builder
The resulting installer tarball can then be found under
`./result/tarball/nixos-wsl-installer.tar.gz`.
```

You can also build a rootfs tarball without wrapping it in the installer
by replacing `installer` with `tarball` in the above commands. The
rootfs tarball can then be found under
`./result/tarball/nixos-wsl-x86_64-linux.tar.gz`.
The resulting tarball can then be found under `nixos-wsl.tar.gz`.

## Design

Getting NixOS to run under WSL requires some workarounds:

### systemd support

WSL comes with its own (non-substitutable) init system while NixOS uses
systemd. Simply starting systemd later on does not work out of the box,
because systemd as system instance refuses to start if it is not PID 1.
This unfortunate combination is resolved in two ways:

- the user\'s default shell is replaced by a wrapper script that acts
is init system and then drops to the actual shell
- systemd is started in its own PID namespace; therefore, it is PID 1.
The shell wrapper (see above) enters the systemd namespace before
dropping to the shell.

### Installer

Usually WSL distributions ship as a tarball of their root file system.
These tarballs however, can not contain any hard-links due to the way
they are unpacked by WSL, resulting in an \"Unspecified Error\". By
default some Nix-derivations will contain hard-links when they are
built. This results in system tarballs that can not be imported into
WSL. To circumvent this problem, the rootfs tarball is wrapped in that
of a minimal distribution (the installer), that is packaged without any
hard-links. When the installer system is started for the first time, it
overwrites itself with the contents of the rootfs tarball.
- instead of directly loading systemd, we use a small shim that runs the NixOS activation scripts first
- some additional binaries required by WSL's internal tooling are symlinked to FHS paths on activation

Running on older WSL versions also requires a workaround to spawn systemd by hijacking the root shell and
spawning a container with systemd inside. This method of running things is deprecated and not recommended,
however still available as `nixos-wsl-legacy.tar.gz` or via `wsl.nativeSystemd = false`.

## License

Expand Down
4 changes: 2 additions & 2 deletions checks/username.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

let
baseModule = { ... }: {
imports = [ ../configuration.nix ];

imports = [ ../modules ];
wsl.enable = true;
wsl.defaultUser = "nixos";
};
changedUsername = { lib, ... }: {
wsl.defaultUser = lib.mkForce "different";
Expand Down
32 changes: 0 additions & 32 deletions configuration.nix

This file was deleted.

24 changes: 19 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@
};
nixosModules.default = self.nixosModules.wsl;

nixosConfigurations.mysystem = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
];
nixosConfigurations = {
modern = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.default
{ wsl.enable = true; }
];
};

legacy = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.default
{
wsl.enable = true;
wsl.nativeSystemd = false;
}
];
};
};

} //
Expand Down
Loading

0 comments on commit c92a7ee

Please sign in to comment.