This builds a NixOS system, copies it into a tmpfs, then instructs systemd to shut down the main system and switch into the new one (without replacing the kernel).
Created
May 27, 2020 07:45
-
-
Save lheckemann/d07efcbef2a2232b6dee2045a80c89aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -exuo pipefail | |
mkdir -p /tmp/new-root-tmpfs | |
findmnt /tmp/new-root-tmpfs || mount -t tmpfs tmpfs /tmp/new-root-tmpfs | |
system=$(nix-build '<nixpkgs/nixos>' --arg configuration '{lib, ...}: | |
let ifExists = p: lib.optional (lib.pathExists p) p; in { | |
boot.supportedFilesystems = ["zfs"]; | |
services.openssh.enable = true; | |
networking.hostId = "23234242"; | |
networking.hostName = "nixos-tmpfs"; | |
networking.wireless.enable = true; | |
networking.wireless.networks.ssid.psk = "password"; | |
fileSystems."/" = { | |
device = "none"; | |
fsType = "tmpfs"; | |
}; | |
boot.loader.grub.enable = false; | |
users.users.root.openssh.authorizedKeys.keyFiles = | |
ifExists /etc/ssh/authorized_keys.d/root | |
++ ifExists /root/.ssh/authorized_keys; | |
}' -A config.system.build.toplevel) | |
nix copy $system --to /tmp/new-root-tmpfs --no-require-sigs | |
ln -s $system /tmp/new-root-tmpfs/system | |
mkdir -p /tmp/new-root-tmpfs/etc | |
rsync /etc/ssh/*host* /tmp/new-root-tmpfs/etc/ssh/ | |
[[ $(read -p "Switch into the tmpfs system? ") = y ]] && systemctl switch-root /tmp/new-root-tmpfs /system/init |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment