### Setup example project environment Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md Commands to create a project directory and pin Nixpkgs using npins. ```shell-session $ mkdir fileset $ cd fileset $ nix-shell -p npins --run "npins init --bare; npins add github nixos nixpkgs --branch nixos-23.11" ``` -------------------------------- ### Create a Nix shell with multiple packages Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Start a Nix shell environment including packages for `git`, `neovim`, and `nodejs`. The initial setup may require downloading dependencies. ```shell $ nix-shell -p git neovim nodejs these 9 derivations will be built: /nix/store/7gz8jyn99kw4k74bgm4qp6z487l5ap06-packdir-start.drv /nix/store/d6fkgxc3b04m85wrhg6j0l5y0ray82l7-packdir-opt.drv /nix/store/da6njv7r0zzc2n54n2j54g2a5sbi4a5i-manifest.vim.drv /nix/store/zs4jb2ybr4rcyzwq0dagg9rlhlc368h6-builder.pl.drv /nix/store/g8sl2xnsshfrz9f39ki94k8p15vp3xd7-vim-pack-dir.drv /nix/store/jmxkg8b1psk52awsvfziy9nq6dwmxmjp-luajit-2.1.0-2022-10-04-env.drv /nix/store/kn83q8yk6ds74zgyklrjhvv5wkv5wmch-python3-3.10.9-env.drv /nix/store/m445wn3vizcgg7syna2cdkkws3kk1gq8-neovim-ruby-env.drv /nix/store/r2wa882mw99c311a4my7hcis9lq3kp3v-neovim-0.8.1.drv these 151 paths will be fetched (186.43 MiB download, 1018.20 MiB unpacked): /nix/store/046zxlxhq4srm3ggafkymx794bn1jksc-bzip2-1.0.8 /nix/store/0p1jxcb7b4p8jhhlf8qnjc4cqwy89460-unibilium-2.1.1 /nix/store/0q4fpnqmg8liqraj7zidylcyd062f6z0-perl5.36.0-URI-5.05 ... [nix-shell:~]$ ``` -------------------------------- ### Execute Startup Commands in Nix Shell Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/declarative-shell.md Use the shellHook attribute to run commands before entering the interactive shell. This example outputs a colorful greeting using the GREETING variable and installed packages. ```nix let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-24.05"; pkgs = import nixpkgs { config = {}; overlays = []; }; in pkgs.mkShellNoCC { packages = with pkgs; [ cowsay lolcat ]; GREETING = "Hello, Nix!"; shellHook = '' echo $GREETING | cowsay | lolcat ''; } ``` -------------------------------- ### Create files for testing Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md Setup commands to create a directory and files for demonstrating file set inclusion. ```shell-session $ mkdir src $ touch build.sh src/select.{c,h} ``` -------------------------------- ### Define a default.nix with callPackage Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/callpackage.md Initial setup for a default.nix file that imports nixpkgs and calls a package derivation. ```nix let pkgs = import { }; in { hello = pkgs.callPackage ./hello.nix { }; } ``` -------------------------------- ### Start a Docker shell with Nix Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md This command starts an interactive Docker shell with Nix pre-installed. ```shell $ docker run -it nixos/nix ``` -------------------------------- ### Lookup Path Examples Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Demonstrates the use of angle bracket syntax to reference file system paths based on nixPath. ```nix ``` ```default /nix/var/nix/profiles/per-user/root/channels/nixpkgs ``` ```nix ``` ```default /nix/var/nix/profiles/per-user/root/channels/nixpkgs/lib ``` -------------------------------- ### Nix Build Command Example Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Illustrates the command used to build a derivation named 'icat' from its Nix expression. ```console $ nix-build -A icat ``` -------------------------------- ### Run the Flask application Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/python-environment.md Command to start the web server within the nix-shell environment. ```shell-session [nix-shell:~]$ python ./myapp.py * Serving Flask app 'myapp' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5000 * Running on http://192.168.1.100:5000 Press CTRL+C to quit ``` -------------------------------- ### Install Nix on Linux (Multi-user) Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md Use this command to install Nix with multi-user support on Linux. Ensure `xz-utils` is installed beforehand. ```shell $ curl -L https://nixos.org/nix/install | sh -s -- --daemon ``` -------------------------------- ### Nix Configuration for Bootable ISO Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/building-bootable-iso-image.md Define a Nix configuration to build a bootable ISO image. This example imports a minimal installation CD configuration and sets the latest Linux kernel. It also forces support for various filesystems. ```nix { pkgs, modulesPath, lib, ... }: { imports = [ "${modulesPath}/installer/cd-dvd/installation-cd-minimal.nix" ]; # use the latest Linux kernel boot.kernelPackages = pkgs.linuxPackages_latest; # Needed for https://github.com/NixOS/nixpkgs/issues/58959 boot.supportedFilesystems = lib.mkForce [ "btrfs" "reiserfs" "vfat" "f2fs" "xfs" "ntfs" "cifs" ]; } ``` -------------------------------- ### Start Nix REPL with Nixpkgs Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/cross-compilation.md Use the `-f` or `--file` flag to specify the Nixpkgs channel when starting `nix repl`. This is required starting with Nix 2.19. ```shell $ nix repl -f '' -I nixpkgs=channel:nixos-23.11 ``` -------------------------------- ### Fix Build Failure with Custom Install Phase Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Shows how to resolve a 'make install' failure by defining a custom 'installPhase' to manually copy the compiled binary to the output directory. ```nix # icat.nix { stdenv, fetchFromGitHub, imlib2, xorg, }: stdenv.mkDerivation { pname = "icat"; version = "v0.5"; src = fetchFromGitHub { owner = "atextor"; repo = "icat"; rev = "v0.5"; sha256 = "0wyy2ksxp95vnh71ybj1bbmqd5ggp13x3mk37pzr99ljs9awy8ka"; }; buildInputs = [ imlib2 xorg.libX11 ]; installPhase = '' mkdir -p $out/bin cp icat $out/bin ''; } ``` -------------------------------- ### Install Nix on Windows (WSL2, Single-user) Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md Use this command for a single-user installation of Nix on Windows via WSL2. This is recommended if systemd support is not enabled. ```shell $ curl -L https://nixos.org/nix/install | sh -s -- --no-daemon ``` -------------------------------- ### Build a package using Nix within Docker Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md This example demonstrates cloning nixpkgs and building the 'hello' package using Nix within a Docker container, exposing the nixpkgs directory. ```shell $ git clone git@github.com:NixOS/nixpkgs $ docker run -it -v $(pwd)/nixpkgs:/nixpkgs nixos/nix bash-5.1# nix-build -I nixpkgs=/nixpkgs -A hello bash-5.1# find ./result # this symlink points to the build package ``` -------------------------------- ### Obtain IP Address with DHCP and Verify Connection Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/installing-nixos-on-a-raspberry-pi.md Install and run dhcpcd to get a proper IP address, then verify internet connectivity by performing a DNS lookup. If DNS resolution fails, check for typos in WiFi credentials and restart wpa_supplicant. ```shell # nix-shell -p dhcpcd # dhcpcd wlan0 # host nixos.org ``` -------------------------------- ### Start a Docker shell with Nix and a working directory Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md This command starts an interactive Docker shell with Nix, mounting the current directory as a workdir. ```shell $ mkdir workdir $ docker run -it -v $(pwd)/workdir:/workdir nixos/nix ``` -------------------------------- ### Complete NixOS Configuration for GNOME VM Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/nixos-configuration-on-vm.md A full `configuration.nix` example enabling GNOME desktop environment, systemd-boot, and user accounts for a graphical VM. ```nix { config, pkgs, ... }: { boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; services.xserver.enable = true; services.xserver.displayManager.gdm.enable = true; services.xserver.desktopManager.gnome.enable = true; users.users.alice = { isNormalUser = true; extraGroups = [ "wheel" ]; initialPassword = "test"; }; system.stateVersion = "24.05"; } ``` -------------------------------- ### Start Local Development Environment Source: https://github.com/nixos/nix.dev/blob/master/CONTRIBUTING.md Use this command to enter the development environment for nix.dev. Ensure you have direnv set up or run nix-shell first. ```shell [nix-shell:nix.dev]$ devmode ``` -------------------------------- ### Start a nested Nix shell with Python Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Enter a nested Nix shell to add `python3` to the current environment. This fetches the package if not already present. ```shell [nix-shell:~]$ nix-shell -p python3 this path will be fetched (11.42 MiB download, 62.64 MiB unpacked): /nix/store/pwy30a7siqrkki9r7xd1lksyv9fg4l1r-python3-3.10.11 copying path '/nix/store/pwy30a7siqrkki9r7xd1lksyv9fg4l1r-python3-3.10.11' from 'https://cache.nixos.org'... [nix-shell:~]$ python --version Python 3.10.11 ``` -------------------------------- ### Curried Function Examples Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Demonstrates handling multiple arguments via nested functions. ```nix x: y: x + y ``` ```default ``` ```nix x: (y: x + y) ``` ```default ``` ```nix let f = x: y: x + y; in f 1 ``` ```default ``` ```nix let f = x: y: x + y; in f 1 2 ``` ```default 3 ``` -------------------------------- ### Basic Python Hello World Example Source: https://github.com/nixos/nix.dev/blob/master/source/contributing/documentation/style-guide.md A standard 'Hello, World!' program in Python. Ensure code samples include the programming language for syntax highlighting. ```python print("Hello, World!") ``` -------------------------------- ### Nix REPL Welcome and Tab Completion Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/cross-compilation.md This shows the welcome message in the Nix REPL and an example of tab completion for cross-compilation packages. ```shell $ nix repl '' -I nixpkgs=channel:nixos-23.11 Welcome to Nix 2.18.1. Type :? for help. Loading ''... Added 14200 variables. nix-repl> pkgsCross. ``` -------------------------------- ### Function Application Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Examples of calling functions with arguments, including passing attribute sets and using parentheses for precedence. ```nix let f = x: x + 1; in f 1 ``` ```default 2 ``` ```nix let f = x: x.a; in f { a = 1; } ``` ```default 1 ``` ```nix let f = x: x.a; v = { a = 1; }; in f v ``` ```default 1 ``` ```nix (x: x + 1) 1 ``` ```default 2 ``` -------------------------------- ### Illustrate Manual Deployment with SCP Source: https://github.com/nixos/nix.dev/blob/master/source/contributing/documentation/style-guide.md This example demonstrates a manual process for deploying secrets to a remote machine using `scp`. It also mentions alternative automation tools. ```text > You will have to deploy secrets to the remote machine. > We chose to show the explicit, manual process using `scp` here, but there are various tools to automate that. ``` -------------------------------- ### Example flake.nix Structure Source: https://github.com/nixos/nix.dev/blob/master/source/concepts/flakes.md A basic `flake.nix` file defining inputs and outputs for a Nix project. This structure is standard for flake entrypoints. ```nix { description = "My example flake"; inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; }; outputs = { self, nixpkgs }: { packages.x86_64-linux = { default = self.packages.x86_64-linux.hello; hello = nixpkgs.legacyPackages.x86_64-linux.hello; }; }; } ``` -------------------------------- ### Basic Nix Module Structure Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/module-system/deep-dive.md An empty Nix module serves as a starting point for defining configurations and options. ```nix { ... }: { } ``` -------------------------------- ### Running a non-flake project with Nix Flakes syntax Source: https://github.com/nixos/nix.dev/blob/master/source/concepts/flakes.md This example shows how to run a command from a non-flake project using Nix Flakes syntax. It demonstrates fetching a tree and executing a command within that context. ```bash nix run github:NixOS/nixpkgs#hello ``` ```bash nix-shell -p "(import (builtins.fetchTree \"github:NixOS/nixpkgs\").outPath { }).hello" --run 'hello' ``` ```bash alias nix-run='run() { $(nix-instantiate --raw --impure --eval --expr "(import {}).lib.getExe (import (builtins.fetchTree \"$(cut -d \"#\" -f 1 <<< \"$1\")\").outPath { }).$(cut -d \"#\" -f 2 <<< \"$1\")"); }; run' ``` -------------------------------- ### Entering Nix Shell with Git and Ripgrep Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Starts a Nix shell environment that provides the 'git' and 'ripgrep' tools, necessary for searching Nixpkgs locally. ```console $ nix-shell -p git ripgrep [nix-shell:~] ``` -------------------------------- ### Build and Run Graphical NixOS VM Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/nixos-configuration-on-vm.md Build the NixOS VM using the specified channel and configuration, then run it to get graphical output. ```shell $ nix-build '' -A vm -I nixpkgs=channel:nixos-24.05 -I nixos-config=./configuration.nix $ ./result/bin/run-nixos-vm ``` -------------------------------- ### Use Imperative Voice for Instructions Source: https://github.com/nixos/nix.dev/blob/master/source/contributing/documentation/style-guide.md When providing instructions, use the imperative voice for clarity. This example shows how to add a package to buildInputs. ```markdown > Add the `python310` package to `buildInputs`. ``` -------------------------------- ### Installing Packages in NixOS Configuration Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/nixos-configuration-on-vm.md Adds `cowsay` and `lolcat` to the system's environment packages. Ensure `pkgs` is available in the scope. ```nix environment.systemPackages = with pkgs; [ cowsay lolcat ]; ``` -------------------------------- ### Custom installPhase with Hooks Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Define a custom `installPhase` in your Nix derivation to control the installation process. Always include `runHook preInstall` and `runHook postInstall` to allow for future customization and maintainability. ```nix installPhase = '' runHook preInstall mkdir -p $out/bin cp icat $out/bin runHook postInstall ''; ``` -------------------------------- ### Minimal NixOS Configuration Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/nixos-configuration-on-vm.md A basic NixOS configuration including bootloader settings and system state version. This is a starting point for building a NixOS system. ```nix { config, pkgs, ... }: { boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; system.stateVersion = "24.05"; } ``` -------------------------------- ### Initialize project directory Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/provisioning-remote-machines.md Create and enter the project directory. ```shell-session mkdir remote cd remote ``` -------------------------------- ### Defining a multi-node NixOS test Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/integration-testing-using-virtual-machines.md An example configuration for a client-server test setup using two virtual machines and a shared test script. ```default let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11"; pkgs = import nixpkgs { config = {}; overlays = []; }; in pkgs.testers.runNixOSTest { name = "client-server-test"; nodes.server = { pkgs, ... }: { networking = { firewall = { allowedTCPPorts = [ 80 ]; }; }; services.nginx = { enable = true; virtualHosts."server" = {}; }; }; nodes.client = { pkgs, ... }: { environment.systemPackages = with pkgs; [ curl ]; }; testScript = '' server.wait_for_unit("default.target") client.wait_for_unit("default.target") client.succeed("curl http://server/ | grep -o \"Welcome to nginx!\"") ''; } ``` -------------------------------- ### Create sample source files Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md Generate dummy text files for testing file set inclusion. ```shell-session $ echo hello > hello.txt $ echo world > world.txt ``` -------------------------------- ### Verify Nix Installation Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md Run this command in a new terminal to verify that Nix has been installed correctly and to check its version. ```shell $ nix --version nix (Nix) 2.11.0 ``` -------------------------------- ### Build and execute the package Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/callpackage.md Commands to build the derivation and execute the resulting binary. ```shell-session $ nix-build $ ./result/bin/hello Hello, world! ``` -------------------------------- ### Build with -I and URL Source: https://github.com/nixos/nix.dev/blob/master/source/reference/pinning-nixpkgs.md Use the -I option to specify a URL for Nixpkgs, pinning it to a specific channel version. ```shell-session nix-build -I nixpkgs=http://nixos.org/channels/nixos-22.11/nixexprs.tar.xz ``` -------------------------------- ### Install Nix on macOS (Multi-user) Source: https://github.com/nixos/nix.dev/blob/master/source/install-nix.md Use this command for the recommended multi-user installation of Nix on macOS. For macOS 15 Sequoia, refer to GitHub issue NixOS/nix#10892 if you encounter user-related errors. ```shell $ curl -L https://nixos.org/nix/install | sh ``` -------------------------------- ### Run Hello Package from Nixpkgs Source: https://github.com/nixos/nix.dev/blob/master/source/concepts/flakes.md Executes the 'hello' package from the Nixpkgs repository using the registry alias 'nixpkgs'. Allows passing custom arguments like '--greeting'. ```bash nix run nixpkgs#hello -- --greeting "hello from flakes" ``` -------------------------------- ### Check for uninstalled programs Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Before using `nix-shell`, these commands show that `cowsay` and `lolcat` are not installed. ```shell $ cowsay no can do The program ‘cowsay’ is currently not installed. $ echo no chance | lolcat The program ‘lolcat’ is currently not installed. ``` -------------------------------- ### Absolute file system paths Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Defines an absolute path starting with a forward slash. ```nix /absolute/path ``` ```default /absolute/path ``` -------------------------------- ### Manual machine control Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/integration-testing-using-virtual-machines.md Commands to start virtual machines within the interactive driver. ```python >>> machine.start() ``` ```python >>> start_all() ``` -------------------------------- ### Initialize marker.nix module Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/module-system/deep-dive.md Create a new module file to hold marker-related configuration. ```nix { lib, config, ... }: { } ``` -------------------------------- ### Enter the Nix shell Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/python-environment.md Command to initialize the declared development environment. ```shell-session $ nix-shell these 2 derivations will be built: /nix/store/5yvz7zf8yzck6r9z4f1br9sh71vqkimk-builder.pl.drv /nix/store/aihgjkf856dbpjjqalgrdmxyyd8a5j2m-python3-3.9.13-env.drv these 93 paths will be fetched (109.50 MiB download, 468.52 MiB unpacked): /nix/store/0xxjx37fcy2nl3yz6igmv4mag2a7giq6-glibc-2.33-123 /nix/store/138azk9hs5a2yp3zzx6iy1vdwi9q26wv-hook ... [nix-shell:~]$ ``` -------------------------------- ### Complete Sample NixOS Configuration Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/nixos-configuration-on-vm.md A comprehensive NixOS configuration file including bootloader settings, user configuration with an initial password, system packages, and state version. Includes a comment for enabling sudo. ```nix { config, pkgs, ... }: { boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; users.users.alice = { isNormalUser = true; extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user. initialPassword = "test"; }; environment.systemPackages = with pkgs; [ cowsay lolcat ]; system.stateVersion = "24.05"; } ``` -------------------------------- ### Evaluate a simple Nix expression Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md A basic example of adding two numbers in the Nix language. ```nix 1 + 2 ``` ```default 3 ``` -------------------------------- ### Install Terraform using Nix Shell Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/deploying-nixos-using-terraform.md Use nix-shell to ensure the Terraform executable is available in your environment. ```shell $ nix-shell -p terraform ``` -------------------------------- ### Get Host Platform Configuration Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/cross-compilation.md Retrieve the platform configuration string for a cross-compilation target using `stdenv.hostPlatform.config`. ```nix nix-repl> pkgsCross.aarch64-multiplatform.stdenv.hostPlatform.config "aarch64-unknown-linux-gnu" ``` -------------------------------- ### Create a Nix shell with specific packages Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Use `nix-shell -p` to create an environment with `cowsay` and `lolcat`. The first invocation may take time to download dependencies. ```shell $ nix-shell -p cowsay lolcat these 3 derivations will be built: /nix/store/zx1j8gchgwzfjn7sr4r8yxb7a0afkjdg-builder.pl.drv /nix/store/h9sbaa2k8ivnihw2czhl5b58k0f7fsfh-lolcat-100.0.1.drv ... [nix-shell:~]$ ``` -------------------------------- ### Start interactive test driver Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/integration-testing-using-virtual-machines.md Launch an interactive Python shell to debug or manually control the test environment. ```shell-session $ $(nix-build -A driverInteractive minimal-test.nix)/bin/nixos-test-driver ``` -------------------------------- ### Build Reference Manuals Source: https://github.com/nixos/nix.dev/blob/master/CONTRIBUTING.md To include the Nix reference manuals during the build process, use the --arg withManuals true flag with either devmode or nix-build. ```shell [nix-shell:nix.dev]$ nix-build -A build --arg withManuals true ``` ```shell [nix-shell:nix.dev]$ devmode --arg withManuals true ``` -------------------------------- ### Initialize direnv for the project Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/direnv.md Configure the directory to use nix-direnv and permit the environment. ```shell-session $ echo "use nix" > .envrc && direnv allow ``` -------------------------------- ### Download NixOS Configuration Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/installing-nixos-on-a-raspberry-pi.md Use this command to download a pre-configured NixOS setup for Raspberry Pi 4 to your system. ```shell # curl -L https://tinyurl.com/tutorial-nixos-install-rpi4 > /etc/nixos/configuration.nix ``` -------------------------------- ### Passing both pkgs and lib as arguments Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Illustrates the pattern of passing both arguments to a function for clarity. ```nix { pkgs, lib, ... }: # ... multiple uses of `pkgs` # ... multiple uses of `lib` ``` -------------------------------- ### Initialize dependencies with npins Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/provisioning-remote-machines.md Initialize npins and add the required nix-community repositories. ```shell-session $ nix-shell -p npins [nix-shell:remote]$ npins init [nix-shell:remote]$ npins add github nix-community disko [nix-shell:remote]$ npins add github nix-community nixos-anywhere ``` -------------------------------- ### Use programs within a Nix shell Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Once inside the Nix shell created with `nix-shell -p`, you can use the installed programs. ```shell [nix-shell:~]$ cowsay Hello, Nix! | lolcat ``` -------------------------------- ### Define NixOS test structure Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/integration-testing-using-virtual-machines.md Initial setup for a NixOS test using a pinned version of Nixpkgs and an empty configuration. ```nix let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11"; pkgs = import nixpkgs { config = {}; overlays = []; }; in pkgs.testers.runNixOSTest { # ... } ``` -------------------------------- ### Create shell.nix for development environment Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/dependency-management.md Create a `shell.nix` file to easily enter the development environment defined in your `default.nix`. ```nix (import ./. {}).shell ``` -------------------------------- ### Import original default.nix with new sources Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/dependency-management.md Create a `default.nix` in a new directory that imports the original project's `default.nix` but uses the newly defined `npins` sources. ```nix import ../default.nix { sources = import ./npins; } ``` -------------------------------- ### Build reverse dependencies with nixpkgs-review Source: https://github.com/nixos/nix.dev/blob/master/source/guides/faq.md Use nixpkgs-review to find and build the reverse dependencies of a package. Ensure nixpkgs-review is installed in your environment. ```shell $ nix-shell -p nixpkgs-review --run "nixpkgs-review wip" ``` -------------------------------- ### Start/Stop Nix Daemon on macOS Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/distributed-builds-setup.md Commands to stop and start the Nix daemon on macOS using launchctl. Root privileges are required. ```shell # sudo launchctl stop org.nixos.nix-daemon # sudo launchctl start org.nixos.nix-daemon ``` -------------------------------- ### Initial Nix Build Output Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md The initial output of `nix-build` shows the derivation being built and the final store path. This output is typical when a derivation is built for the first time. ```default $ nix-build trace: /home/user/fileset trace: - hello.txt (regular) this derivation will be built: /nix/store/3ci6avmjaijx5g8jhb218i183xi7bi2n-fileset.drv ... 'hello.txt' -> '/nix/store/sa4g6h13v0zbpfw6pzva860kp5aks44n-fileset/hello.txt' ... /nix/store/sa4g6h13v0zbpfw6pzva860kp5aks44n-fileset ``` -------------------------------- ### Passing pkgs as a function argument Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md Demonstrates defining a function that expects pkgs as an argument and evaluating it via nix-instantiate. ```nix { pkgs, ... }: pkgs.lib.strings.removePrefix "no " "no true scotsman" ``` ```default ``` ```shell-session $ nix-instantiate --eval file.nix --arg pkgs 'import {}' "true scotsman" ``` -------------------------------- ### Fetch SHA256 hash for tarball Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Use `nix-prefetch-url` with `--unpack` and `--type sha256` to get the correct hash for the source tarball. ```console $ nix-prefetch-url --unpack https://github.com/atextor/icat/archive/refs/tags/v0.5.tar.gz --type sha256 path is '/nix/store/p8jl1jlqxcsc7ryiazbpm7c1mqb6848b-v0.5.tar.gz' 0wyy2ksxp95vnh71ybj1bbmqd5ggp13x3mk37pzr99ljs9awy8ka ``` -------------------------------- ### Basic Nix Derivation with fetchzip Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md This Nix expression defines a derivation for the 'hello' package. It uses `stdenv.mkDerivation` and `fetchzip` to download and build the source code, specifying the package name, version, and a sha256 hash for the source archive. ```nix # hello.nix { stdenv, fetchzip, }: stdenv.mkDerivation { pname = "hello"; version = "2.12.1"; src = fetchzip { url = "https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz"; sha256 = "sha256-1kJjhtlsAkpNB7f6tZEs+dbKd8z7KoNHyDHEJ0tmhnc="; }; } ``` -------------------------------- ### Skeleton Nix Derivation Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md A basic Nix derivation function that takes stdenv as an argument and produces a derivation. This serves as a starting point for packaging software. ```nix { stdenv }: stdenv.mkDerivation { } ``` -------------------------------- ### Deploy NixOS System with nixos-anywhere Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/provisioning-remote-machines.md Builds the NixOS configuration and disko script, then deploys them to a target host using nixos-anywhere. Requires replacing 'target-host' with the actual hostname or IP. ```shell toplevel=$(nixos-rebuild build --no-flake) diskoScript=$(nix-build -E "((import {}).nixos [ ./configuration.nix ]).diskoScript") nixos-anywhere --store-paths "$diskoScript" "$toplevel" root@target-host ``` -------------------------------- ### Avoid Conversational Tone in Instructions Source: https://github.com/nixos/nix.dev/blob/master/source/contributing/documentation/style-guide.md Refrain from using a conversational tone when giving instructions, as it can distract from the content. This example illustrates an undesirable conversational phrasing. ```markdown > Going forward, let’s now add the `python310` package to `buildInputs` as we have seen in the previous tutorial. ``` -------------------------------- ### Check package versions in Nix shell Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/first-steps/ad-hoc-shell-environments.md Verify the exact versions of `git`, `nvim`, and `npm` provided by the Nix environment, even if other versions are installed system-wide. ```shell [nix-shell:~]$ which git /nix/store/3cdi52xh6lk3h1fb51jkxs3p561p37wg-git-2.38.3/bin/git [nix-shell:~]$ git --version git version 2.38.3 [nix-shell:~]$ which nvim /nix/store/ynskzgkf07lmrrs3cl2kzr9ah487lwab-neovim-0.8.1/bin/nvim [nix-shell:~]$ nvim --version | head -1 NVIM v0.8.1 [nix-shell:~]$ which npm /nix/store/q12w83z0i5pi1y0m6am7qmw1r73228sh-nodejs-18.12.1/bin/npm [nix-shell:~]$ npm --version 8.19.2 ``` -------------------------------- ### Initialize lib.fileset in Nix REPL Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md Access the lib.fileset library within an interactive Nix session. ```shell-session $ nix repl -f channel:nixos-23.11 ... nix-repl> fs = lib.fileset ``` -------------------------------- ### NixOS Configuration File Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/deploying-nixos-using-terraform.md A basic NixOS configuration file that includes modules for virtualisation on Amazon images. It serves as a starting point for customizing your NixOS instance. ```nix { config, lib, pkgs, ... }: { imports = [ ]; # Open https://search.nixos.org/options for all options } ``` -------------------------------- ### Import binary cache module Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/binary-cache-setup.md Adds the binary cache configuration module to the main system configuration. ```nix { config, ... }: { imports = [ ./binary-cache.nix ]; # ... } ``` -------------------------------- ### Build and Execute Packaged Script Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/module-system/deep-dive.md Commands to build the Nix derivation and execute the resulting binary. ```console nix-build eval.nix -A config.scripts.output ./result/bin/map ``` -------------------------------- ### Example of an ambiguous Nix expression Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nix-language.md This snippet demonstrates a function that takes an attribute set as an argument, highlighting the difficulty in determining the expected types and structures of its inputs. ```nix { x, y, z }: (x y) z.a ``` -------------------------------- ### Build with -I and Channel Shorthand Source: https://github.com/nixos/nix.dev/blob/master/source/reference/pinning-nixpkgs.md Use the -I option with the channel shorthand syntax to specify Nixpkgs. ```shell-session nix-build -I nixpkgs=channel:nixos-22.11 ``` -------------------------------- ### Define Package Build in build.nix Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/sharing-dependencies.md Define a Nix package build in `build.nix`, specifying its dependencies using `buildInputs`. This example uses `cowsay` as a build-time dependency. ```nix { cowsay, runCommand }: runCommand "cowsay-output" { buildInputs = [ cowsay ]; } '' cowsay Hello, Nix! > $out '' ``` -------------------------------- ### Build with NIX_PATH and URL Source: https://github.com/nixos/nix.dev/blob/master/source/reference/pinning-nixpkgs.md Set the NIX_PATH environment variable to specify a URL for Nixpkgs, pinning it to a specific channel version. ```shell-session NIX_PATH=nixpkgs=http://nixos.org/channels/nixos-22.11/nixexprs.tar.xz nix-build ``` -------------------------------- ### Test NixOS Disk Layout Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/provisioning-remote-machines.md Builds and runs an installation test for the NixOS system within a virtual machine using the disko module's installTest attribute. ```shell nix-build -E "((import {}).nixos [ ./configuration.nix ]).installTest" ``` -------------------------------- ### Test Distributed Builds Locally Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/distributed-builds-setup.md Build a derivation on a remote machine to test distributed build setup. The `--max-jobs 0` flag forces remote building. ```shell $ nix-build --max-jobs 0 -E "$(cat << EOF (import {}).writeText "test" "$(date)" EOF )" ``` -------------------------------- ### Define Disk Device Path in Nix Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/provisioning-remote-machines.md Defines the path for the disk block device. This variable is used throughout the configuration for partitioning, formatting, and boot loader setup. ```nix let diskDevice = "/dev/sda"; sources = import ./npins; in ``` -------------------------------- ### Configure fetchFromGitHub with source details Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md Integrate `fetchFromGitHub` into your Nix derivation, providing owner, repo, revision, and the SHA256 hash. ```nix # icat.nix { stdenv, fetchFromGitHub, }: stdenv.mkDerivation { pname = "icat"; version = "v0.5"; src = fetchFromGitHub { owner = "atextor"; repo = "icat"; rev = "v0.5"; sha256 = "0wyy2ksxp95vnh71ybj1bbmqd5ggp13x3mk37pzr99ljs9awy8ka"; }; } ``` -------------------------------- ### Define a Nix Package with callPackage Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/packaging-existing-software.md This snippet shows how to define a package named 'hello' using `pkgs.callPackage` in a `default.nix` file. It imports nixpkgs and specifies the derivation to use. ```nix let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-24.05"; pkgs = import nixpkgs { config = {}; overlays = []; }; in { hello = pkgs.callPackage ./hello.nix { }; } ``` -------------------------------- ### Refactor default.nix with inputsFrom (Diff) Source: https://github.com/nixos/nix.dev/blob/master/source/guides/recipes/sharing-dependencies.md Demonstrates refactoring `default.nix` to move the `build` attribute into the `let` binding and use `inputsFrom` to share its dependencies with the `shell` environment. ```diff let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11"; pkgs = import nixpkgs { config = {}; overlays = []; }; + build = pkgs.callPackage ./build.nix {}; in { - build = pkgs.callPackage ./build.nix {}; + inherit build; shell = pkgs.mkShellNoCC { + inputsFrom = [ build ]; }; } ``` -------------------------------- ### Download and Uncompress NixOS Image Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/nixos/installing-nixos-on-a-raspberry-pi.md Use nix-shell to install wget and zstd, then download and uncompress the NixOS AArch64 image for Raspberry Pi. Ensure you have enough disk space for the uncompressed image. ```shell $ nix-shell -p wget zstd [nix-shell:~]$ wget https://hydra.nixos.org/build/226381178/download/1/nixos-sd-image-23.11pre500597.0fbe93c5a7c-aarch64-linux.img.zst [nix-shell:~]$ unzstd -d nixos-sd-image-23.11pre500597.0fbe93c5a7c-aarch64-linux.img.zst ``` -------------------------------- ### Initialize Git repository for tracking Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/working-with-local-files.md Commands to initialize a Git repository and manage tracked files. ```shell-session $ git init Initialized empty Git repository in /home/user/fileset/.git/ $ git add -A $ git reset src/select.o result ``` -------------------------------- ### Cross Compile by Importing with `crossSystem` Source: https://github.com/nixos/nix.dev/blob/master/source/tutorials/cross-compilation.md Configure Nixpkgs to build all packages for a specific host platform by passing `crossSystem` during import. This sets up the environment so that `pkgs.hello` refers to the cross-compiled version. ```nix let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/release-23.11"; pkgs = import nixpkgs { crossSystem = { config = "aarch64-unknown-linux-gnu"; }; }; in pkgs.hello ```