Git Product home page Git Product logo

nixfiles's Introduction

nixfiles

Some of my .nix files

Installing: Flake Edition

The configurations have been adapted to use flakes now, so some of the call flows have been abstracted and reversed to allow for the stateless/reproduceability requirements of flakes.

In order to maintain some sensitive data outside the repo, the NixOS flake is designed to be called into by another minimal flake. For convenience, there is a template for this flake in this repo:

$ nix flake init -t $NIXFILES_PATH/templates#nixos-device

Installing: Old Pre-flake Edition

Disk Setup

Rough sketch of the expected disk layout with full-disk encryption.

Some changes to the sketch for a more realistic scenario:

  • Swap partition should be at least as big as the machine's RAM to support hibernate-to-disk. Otherwise, it can be omitted altogether in place of a swapfile.
  • Boot partition can be made bigger to support an embedded recovery image (4-6 GB).

NOTE: If trying in a VM, make sure to use a SCSI virtual disk (instead of HDA) and UEFI enabled.

# Setup partition layout
# Swap should be >RAM size if you're going to use hibernate
parted /dev/sda -- mklabel gpt
parted /dev/sda -- mkpart ESP fat32 1MB 512MB  # boot
parted /dev/sda -- set 1 boot on
parted /dev/sda -- mkpart primary 512MB -1GB  # root
parted /dev/sda -- mkpart primary linux-swap -1GB 100%  # swap

# Encrypt the partitions
# Swap partition is also encrypted, so our hibernate state is encrypted.
# We use luks1 (instead of luks2) because grub2 only supports luks1 for now.
# Follow: https://github.com/NixOS/nixpkgs/issues/65375 for LUKS2 on Grub
# To convert, see: https://cryptsetup-team.pages.debian.net/cryptsetup/encrypted-boot.html
cryptsetup luksFormat --type luks1 /dev/sda2  # Enter password
cryptsetup luksFormat --type luks1 /dev/sda3  # Enter the same password

# Good time to add a key file, if we want to do that:
#   dd if=/dev/urandom of=cryptroot.key bs=1 count=4096
#   chmod 0400 cryptroot.key
#   cryptsetup luksAddKey /dev/sda2 cryptroot.key
#   cryptsetup luksAddKey /dev/sda3 cryptroot.key

# Open the encrypted partitions
cryptsetup open /dev/sda2 cryptroot
cryptsetup open /dev/sda3 cryptswap

# Format the underlying partitions
mkfs.fat -F 32 -n EFI /dev/sda1  # Unencrypted EFI partition
mkswap /dev/mapper/cryptswap
mkfs.btrfs /dev/mapper/cryptroot
mount -o defaults,noatime,compress=lzo,autodefrag /dev/mapper/cryptroot /mnt

# Create volumes on the btrfs root
btrfs subvolume create /mnt/@rootnix
btrfs subvolume create /mnt/@home

# Remount with new volumes
umount /mnt
mount -o compress=lzo,subvol=@rootnix /dev/mapper/cryptroot /mnt
mkdir -p /mnt/boot /mnt/home
mount -o compress=lzo,subvol=@home /dev/mapper/cryptroot /mnt/home
mkdir /mnt/boot/efi
mount /dev/sda1 /mnt/boot/efi

# Enable swap if you're using nixos-generate-config to auto-detect mounts
swapon /dev/mapper/cryptswap

Resume an existing disk setup:

cryptsetup open /dev/sda2 cryptroot  # Enter password
cryptsetup open /dev/sda3 cryptswap  # Enter password

mount -o compress=lzo,subvol=@rootnix /dev/mapper/cryptroot /mnt
mount -o compress=lzo,subvol=@home /dev/mapper/cryptroot /mnt/home
mount /dev/sda1 /mnt/boot/efi

NixOS Setup from another distro

If you're installing from inside another distro, you can use these instructions: https://nixos.org/nixos/manual/index.html#sec-installing-from-other-distro

In Arch, using the aur/nix package does not work.

If we need to add hardware-specific configuration imports, we'll need nixos-hardware (setup instructions). The nix environment activator only includes the nixpkgs channel in the NIX_PATH by default, so we'll need to add that too.

# Activate the nix environment
. $HOME/.nix-profile/etc/profile.d/nix.sh

# Add the nixos-hardware channel
nix-channel --add https://github.com/NixOS/nixos-hardware/archive/master.tar.gz nixos-hardware
nix-channel --update nixos-hardware

# Add the new channel to our NIX_PATH
export NIX_PATH=${NIX_PATH}:${NIX_PATH//nixpkgs/nixos-hardware}

Some other notes for installing from another distro (doesn't apply for a normal install):

  • ${disk.efi} should be mounted to /mnt/boot/efi (or whatever the root prefix is).

After that, off we go:

sudo groupadd -g 30000 nixbld
sudo useradd -u 30000 -g nixbld -G nixbld nixbld
sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt

NixOS Setup from scratch (in a VM)

curl -Ls "https://github.com/shazow/nixfiles/archive/master.zip" -o nixfiles.zip
unzip nixfiles.zip

mkdir /mnt/etc
mv nixfiles-master /mnt/etc/nixos

cd /mnt/etc/nixos
echo \"$(mkpasswd -m sha-512)\" > .hashedPassword.nix
chmod 400 .hashedPassword.nix

cat > disk.nix << EOF
{
  cryptroot = "/dev/sda2";
  cryptswap = "/dev/sda3";
  efi = "/dev/sda1";
}
EOF

cp hosts/example.nix configuration.nix
echo "Edit configuration.nix ... Some of the paths are wrong here, need to fix."

nixos-install --root /mnt

References

Big thanks to my friend group of NixOS pioneers who paved through the unknowns, and answered many questions along the way.

Full Disk Encryption (FDE)

nixfiles's People

Contributors

mveytsman avatar shazow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nixfiles's Issues

Install failure

Trying to do:

https://nixos.org/nixos/manual/index.html#sec-installing-from-other-distro

Using hosts/shazowic-corvus.nix as configuration.nix (the README in the repo has more details about the general setup). Also reproduced with a trivial configuration.nix (see below)

I get this error:

$ nixos-install --no-bootloader --root /mnt/rootnix --show-trace
building the configuration in /mnt/rootnix/etc/nixos/configuration.nix...
error: while setting up the build environment: getting attributes of path '': No such file or directory

How do I debug where the '' is coming from?

Edit: Seems it's a problem with nixos-install when using the nix installed on Arch on aur/nix, see comments.

Disable cpu-throttling fix

The current approach doesn't work...

# TODO: Figure out how to disable these...
# Disable the "throttling bug fix" -_- https://github.com/NixOS/nixos-hardware/blob/master/common/pc/laptop/cpu-throttling-bug.nix
#systemd.timers.cpu-throttling.enable = false;
#systemd.services.cpu-throttling.enable = false;

error: The option `systemd.services.cpu-throttling.enable' has conflicting definitions, in `/mnt/rootnix/etc/nixos/configuration.nix' and `/nix/var/nix/profiles/per-user/root/channels/nixos-hardware/common/pc/laptop/cpu-throttling-bug.nix'.

Not sure how I'm supposed to override this service while still using the hardware definition.

Questions

  • What's the difference between users.extraUsers.* and users.users.*
    Answer: Legacy thing? extraUsers is an alias these days.
  • Best way to manage private keys (ssh, etc)?
    Answer: Can do a secrets import thing: https://github.com/attente/dotfiles/blob/master/configuration.nix#L5
  • How do nix-managed configs work? (example) How does NixOS know to transform it from declarative to the app-native config format?
  • Is it possible or bad to maintain app-native config formats?
    Answer: https://nixos.wiki/wiki/Wrappers_vs._Dotfiles - can use things like home.file.".config/i3blocks/config".source = "${my-dotfile-dir}/i3blocks.conf"
    Another option is to use something like foo.extraConfig = builtins.readFile ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.