Skip to content

Instantly share code, notes, and snippets.

@Isabaellchen
Last active August 28, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Isabaellchen/8e489f248bf442f29bdfea143df955e7 to your computer and use it in GitHub Desktop.
Save Isabaellchen/8e489f248bf442f29bdfea143df955e7 to your computer and use it in GitHub Desktop.
A nixos install script for linux live sessions, to have nixos installed as the primary OS. Expects a configuration.nix (like https://gist.github.com/Isabaellchen/ffb8ba6646ea730a0dd334fe336ff173 ) to be in the same folder.
#!/bin/bash
##
## A nixos install script for linux live sessions,
## to have nixos installed as the primary OS.
##
## Derived from the nixos manual
## https://nixos.org/nixos/manual/index.html#sec-installing-from-other-distro
## and
## https://nixos.org/nixos/manual/index.html#sec-installation
##
# From within a linux live session:
# Get a nix shell
curl https://nixos.org/nix/install | sh
. $HOME/.nix-profile/etc/profile.d/nix.sh
# Set up a package channel (assuming v19.03)
nix-channel --add https://nixos.org/channels/nixos-19.03 nixpkgs
# Install basic setup tools
nix-env -iE "_: with import <nixpkgs/nixos> { configuration = {}; }; with config.system.build; [ nixos-generate-config nixos-install nixos-enter manual.manpages ]"
# Partition the disk (assuming 16GB swap for 8GB RAM)
sudo parted /dev/sda -- mklabel gpt
sudo parted /dev/sda -- mkpart primary 512MiB -16GB
sudo parted /dev/sda -- mkpart primary linux-swap -16GB 100%
sudo parted /dev/sda -- mkpart ESP fat32 1MiB 512MiB
sudo parted /dev/sda -- set 3 boot on
# Format the partitions
sleep 1
sudo mkfs.ext4 -L nixos /dev/sda1
sudo mkswap -L swap /dev/sda2
sudo mkfs.fat -F 32 -n BOOT /dev/sda3
# Mount partitions and enable the swap
sleep 1
sudo mount /dev/disk/by-label/nixos /mnt
sudo mkdir -p /mnt/boot
sudo mount /dev/disk/by-label/BOOT /mnt/boot
sudo swapon /dev/sda2
# Generate hardware-configuration.nix and configuration.nix
sudo `which nixos-generate-config` --root /mnt
# Overwrite the default configuration.nix
sudo cp configuration.nix /mnt/etc/nixos/configuration.nix
# Add a user and group for the build process
sudo groupadd -g 30000 nixbld
sudo useradd -u 30000 -g nixbld -G nixbld nixbld
# Do the installation. It will finish by asking to set a new root password
sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment