summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorK900 <me@0upti.me>2023-08-09 09:46:51 +0300
committerK900 <me@0upti.me>2023-08-15 22:27:46 +0300
commit63c6c8fa7114e3f6149a34d5df5ace6b9135e47a (patch)
tree5ce782eab89ff9d4f2dcd79752e22cc4e18dfe47
parentf7a95a37306c46b42e9ce751977c44c752fd5eca (diff)
feat: add a recovery shell
-rw-r--r--README.md20
-rw-r--r--modules/wsl-distro.nix19
2 files changed, 39 insertions, 0 deletions
diff --git a/README.md b/README.md
index 45a948b..e2dad8f 100644
--- a/README.md
+++ b/README.md
@@ -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