Skip to content

Instantly share code, notes, and snippets.

@nh2 nh2/nixos-rootfs.nix
Last active Apr 19, 2019

What would you like to do?
Example of how to build a NixOS root file system
# Build with:
# NIX_PATH=nixpkgs=$HOME/src/nixpkgs nix-build --no-link '<nixpkgs/nixos>' -A -I nixos-config=thisfile.nix
# You can also use
# -A
# to build something you can browse locally (that uses symlinks into your nix store).
{config, pkgs, ...}:
# We need no bootloader, because the Chromebook can't use that anyway.
boot.loader.grub.enable = false;
fileSystems = {
# Mounts whatever device has the NIXOS_ROOT label on it as /
# (but it's only really there to make systemd happy, so it wont try to remount stuff).
"/".label = "NIXOS_ROOT";
# Trim locales a lot to save disk space (but sacrifice translations).
# Unfortunately currently only gets rid of the large `glibc-locales`
# package (120 MB as of writing);
# the individual packages still have all their big `.mo` files.
i18n.supportedLocales = [ (config.i18n.defaultLocale + "/UTF-8") ]; = pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
storeContents = [
symlink = "/bin/init";
object = "${}/init";
contents = [];
compressCommand = "cat";
compressionExtension = "";
# Install new init script; this ensures that /init is updated after every
# `nixos-rebuild` run on the machine (the kernel can run init from a
# symlink).
system.activationScripts.installInitScript = ''
ln -fs $systemConfig/init /bin/init
boot.postBootCommands =
# Import Nix DB, so that nix commands work and know what's installed.
# The `rm` ensures it's done only once; `/nix-path-registration`
# is a file created in the tarball by `make-system-tarball.nix`.
if [ -f /nix-path-registration ]; then
${config.nix.package.out}/bin/nix-store --load-db < /nix-path-registration && rm /nix-path-registration
# Create the system profile to make nixos-rebuild happy
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
# Configuration of the contents of the NixOS system below:
# Empty root password so people can easily use the live image.
# Note that changing this requires *wiping* the root file system
# (or at least /etc/shadow?) on the SD card because
# the password `/etc/shadow` is created from this only *once*;
# see <nixos/modules/config/>.
users.users.root.password = "";
# Disable DHCP so that the boot doesn't hang for it.
networking.dhcpcd.enable = false;
# Turn on nginx as an example
services.nginx.enable = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.