Skip to content

Commit

Permalink
feat: add a recovery shell
Browse files Browse the repository at this point in the history
  • Loading branch information
K900 committed Aug 9, 2023
1 parent f7a95a3 commit 2136658
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions modules/wsl-distro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ let
exec ${pkgs.bashInteractive}/bin/sh "$@"
'';

nixos-enter' = config.system.build.nixos-enter.overrideAttrs(_: {
runtimeShell = "/bin/bash";
});

recovery = pkgs.writeScriptBin "nixos-wsl-recovery" ''
#! /bin/bash
mount -o remount,rw /mnt/wslg/distro
exec /mnt/wslg/distro/${nixos-enter'}/bin/nixos-enter --root /mnt/wslg/distro "$@"
'';

cfg = config.wsl;
in
{
Expand Down Expand Up @@ -125,6 +135,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
Expand Down

0 comments on commit 2136658

Please sign in to comment.