diff options
| author | nzbr <mail@nzbr.de> | 2023-08-20 17:03:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-20 17:03:55 +0200 |
| commit | faab3194692c5b6b351e33fc8d5e7f15f22d1d15 (patch) | |
| tree | 5ce782eab89ff9d4f2dcd79752e22cc4e18dfe47 | |
| parent | f7a95a37306c46b42e9ce751977c44c752fd5eca (diff) | |
| parent | 63c6c8fa7114e3f6149a34d5df5ace6b9135e47a (diff) | |
Merge pull request #274 from nix-community/recovery-shell
feat: add a recovery shell
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | modules/wsl-distro.nix | 19 |
2 files changed, 39 insertions, 0 deletions
@@ -40,6 +40,26 @@ If you want to make NixOS your default distribution, you can do so with wsl -s NixOS ``` +## Troubleshooting + +A recovery shell can be started with + +```sh +wsl -d NixOS --system --user root -- /mnt/wslg/distro/bin/nixos-wsl-recovery +``` + +This will load the WSL "system" distribution, activate your configuration, +then chroot into your NixOS system, similar to what `nixos-enter` would do +on a normal NixOS install. + +You can choose an older generation to load with + +```sh +wsl -d NixOS --system --user root -- /mnt/wslg/distro/bin/nixos-wsl-recovery --system /nix/var/nix/profiles/system-42-link +``` + +(note that the path is relative to the new root) + ## Building your own system tarball This requires access to a system that already has Nix installed. Please refer to the [Nix installation guide](https://nixos.org/guides/install-nix.html) if that\'s not the case. diff --git a/modules/wsl-distro.nix b/modules/wsl-distro.nix index ac763d7..0c5a15f 100644 --- a/modules/wsl-distro.nix +++ b/modules/wsl-distro.nix @@ -8,6 +8,22 @@ let exec ${pkgs.bashInteractive}/bin/sh "$@" ''; + nixos-enter' = config.system.build.nixos-enter.overrideAttrs (_: { + runtimeShell = "/bin/bash"; + }); + + recovery = pkgs.writeScriptBin "nixos-wsl-recovery" '' + #! /bin/sh + if [ -f /etc/NIXOS ]; then + echo "nixos-wsl-recovery should only be run from the WSL system distribution." + echo "Example:" + echo " wsl --system --distribution NixOS --user root -- /nix/var/nix/profiles/system/bin/nixos-wsl-recovery" + exit 1 + fi + mount -o remount,rw /mnt/wslg/distro + exec /mnt/wslg/distro/${nixos-enter'}/bin/nixos-enter --root /mnt/wslg/distro "$@" + ''; + cfg = config.wsl; in { @@ -125,6 +141,9 @@ in ln -sf /init /bin/wslpath ln -sf ${cfg.binShPkg}/bin/sh /bin/sh ln -sf ${pkgs.util-linux}/bin/mount /bin/mount + + # needs to be a copy, not a symlink, to be executable from outside + cp -f ${recovery}/bin/nixos-wsl-recovery /bin/nixos-wsl-recovery ''); update-entrypoint.text = '' mkdir -p /nix/nixos-wsl |
