Skip to content

Instantly share code, notes, and snippets.

@nh2
Last active November 16, 2024 04:08
Show Gist options
  • Save nh2/b7d285a7530603c2fe0b426fbb3da350 to your computer and use it in GitHub Desktop.
Save nh2/b7d285a7530603c2fe0b426fbb3da350 to your computer and use it in GitHub Desktop.
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 config.system.build.tarball -I nixos-config=thisfile.nix
# You can also use
# -A config.system.build.toplevel
# 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") ];
system.build.tarball = pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
storeContents = [
{
symlink = "/bin/init";
object = "${config.system.build.toplevel}/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
fi
''
+
# 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/update-users-groups.pl>.
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