### Example Nix Manifest File Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/files/manifest.nix.md This is an example of a manifest.nix file after installing the 'hello' package. It details the package's metadata, output path, system, and type. ```nix [{ meta = { available = true; broken = false; changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v2.12.1"; description = "A program that produces a familiar, friendly greeting"; homepage = "https://www.gnu.org/software/hello/manual/"; insecure = false; license = { deprecated = false; free = true; fullName = "GNU General Public License v3.0 or later"; redistributable = true; shortName = "gpl3Plus"; spdxId = "GPL-3.0-or-later"; url = "https://spdx.org/licenses/GPL-3.0-or-later.html"; }; longDescription = '' GNU Hello is a program that prints "Hello, world!" when you run it. It is fully customizable. ''; maintainers = [{ email = "edolstra+nixpkgs@gmail.com"; github = "edolstra"; githubId = 1148549; name = "Eelco Dolstra"; }]; name = "hello-2.12.1"; outputsToInstall = [ "out" ]; platforms = [ "i686-cygwin" "x86_64-cygwin" "x86_64-darwin" "i686-darwin" "aarch64-darwin" "armv7a-darwin" "i686-freebsd13" "x86_64-freebsd13" "aarch64-genode" "i686-genode" "x86_64-genode" "x86_64-solaris" "js-ghcjs" "aarch64-linux" "armv5tel-linux" "armv6l-linux" "armv7a-linux" "armv7l-linux" "i686-linux" "m68k-linux" "microblaze-linux" "microblazeel-linux" "mipsel-linux" "mips64el-linux" "powerpc64-linux" "powerpc64le-linux" "riscv32-linux" "riscv64-linux" "s390-linux" "s390x-linux" "x86_64-linux" "mmix-mmixware" "aarch64-netbsd" "armv6l-netbsd" "armv7a-netbsd" "armv7l-netbsd" "i686-netbsd" "m68k-netbsd" "mipsel-netbsd" "powerpc-netbsd" "riscv32-netbsd" "riscv64-netbsd" "x86_64-netbsd" "aarch64_be-none" "aarch64-none" "arm-none" "armv6l-none" "avr-none" "i686-none" "microblaze-none" "microblazeel-none" "msp430-none" "or1k-none" "m68k-none" "powerpc-none" "powerpcle-none" "riscv32-none" "riscv64-none" "rx-none" "s390-none" "s390x-none" "vc4-none" "x86_64-none" "i686-openbsd" "x86_64-openbsd" "x86_64-redox" "wasm64-wasi" "wasm32-wasi" "x86_64-windows" "i686-windows" ]; position = "/nix/store/7niq32w715567hbph0q13m5lqna64c1s-nixos-unstable.tar.gz/nixos-unstable.tar.gz/pkgs/applications/misc/hello/default.nix:34"; unfree = false; unsupported = false; }; name = "hello-2.12.1"; out = { outPath = "/nix/store/src1vzij2z0slnakrsbpqpk20389z0k6-hello-2.12.1"; }; outPath = "/nix/store/src1vzij2z0slnakrsbpqpk20389z0k6-hello-2.12.1"; outputs = [ "out" ]; system = "x86_64-linux"; type = "derivation"; }] ``` -------------------------------- ### Build and Install Nix from Source Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/building-source.md Clone the Nix repository, set up the build environment with Meson, compile the code with Ninja, and install the built binaries. The installation path can be customized. ```bash git clone https://github.com/NixOS/nix.git cd nix meson setup build cd build ninja (sudo) ninja install ``` -------------------------------- ### Install Nix Source: https://github.com/nixos/nix/blob/master/HACKING.md Run the installPhase to install Nix to the specified output directory and verify the installation by checking the version. ```console [nix-shell]$ installPhase [nix-shell]$ ./outputs/out/bin/nix --version ``` -------------------------------- ### Install nix-serve Source: https://github.com/nixos/nix/blob/master/doc/manual/source/package-management/binary-cache-substituter.md Installs the `nix-serve` daemon from Nixpkgs. This is the first step to setting up a custom binary cache. ```console $ nix-env --install --attr nixpkgs.nix-serve ``` -------------------------------- ### Get store derivation info Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.15.md Example of how to get information about a store derivation itself. ```shell nix path-info /nix/store/fpq78s2h8ffh66v2iy0q1838mhff06y8-glibc-2.33-78.drv ``` -------------------------------- ### Copying a Store Path to a Remote Machine Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-copy-closure.md This example demonstrates how to copy a specific store path, representing a Nix package (GNU Hello in this case) and its dependencies, to a remote machine named 'itchy.example.org' under the user 'alice'. It first builds the package to get its store path and then uses nix-copy-closure to transfer it. ```shell $ storePath="$(nix-build '' -I nixpkgs=channel:nixpkgs-unstable -A hello --no-out-link)" $ nix-copy-closure --to alice@itchy.example.org "$storePath" copying 5 paths... copying path '/nix/store/h6q8sqsqfbd3252f9gixqn3z282wds7m-xgcc-13.2.0-libgcc' to 'ssh://alice@itchy.example.org'... copying path '/nix/store/imnwvn96lw355giswsk36hx105j4wnpj-libunistring-1.1' to 'ssh://alice@itchy.example.org'... copying path '/nix/store/85301indj7scg34spnfczkz72jgv8wa9-libidn2-2.3.7' to 'ssh://alice@itchy.example.org'... copying path '/nix/store/ypwfsaljwhzw9iffiysxmxnhjj8v7np0-glibc-2.39-31' to 'ssh://alice@itchy.example.org'... copying path '/nix/store/0dklv59zppdsqdvgf0qdvjgzcs5wbwxa-hello-2.12.1' to 'ssh://alice@itchy.example.org'... ``` -------------------------------- ### Show Nix Installer Help Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Displays the available command-line flags and options for the Nix installer script. Useful for customizing the installation process. ```console $ curl -L https://nixos.org/nix/install | sh -s -- --help ``` -------------------------------- ### Example nix-cache-info File Source: https://github.com/nixos/nix/blob/master/doc/manual/source/protocols/binary-cache/nix-cache-info.md This example shows the typical fields and their values in a nix-cache-info file. ```text StoreDir: /nix/store WantMassQuery: 1 Priority: 30 ``` -------------------------------- ### Install Nix with Rust Installer (Beta) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.34.md Use this command to install Nix using the new Rust-based installer. It can be run alongside existing installations. ```bash curl -sSfL https://artifacts.nixos.org/nix-installer | sh -s -- install ``` -------------------------------- ### Install Nix on Single-User (All Platforms) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/upgrading.md Installs the Nix package and cacert for a single-user setup across all platforms using the unstable channel. ```console $ nix-env --install --file '' --attr nix cacert -I nixpkgs=channel:nixpkgs-unstable ``` -------------------------------- ### Install from a store output path Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a package directly from its Nix store output path. ```console $ nix-env --install /nix/store/y3cgx0xj1p4iv9x0pnnmdhr8iyg741vk-gcc-3.4.3 ``` -------------------------------- ### Get store derivation output paths info Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.15.md Example of how to get information about the output paths of a store derivation using the new '^' syntax. ```shell nix path-info /nix/store/fpq78s2h8ffh66v2iy0q1838mhff06y8-glibc-2.33-78.drv^* ``` -------------------------------- ### Simple Store Path Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/protocols/json/store-path.md An example of a simple Nix store path in JSON format. ```json { "path": "/nix/store/abcdefghijklmnopqrstuvwxyz-example-package-1.0" } ``` -------------------------------- ### Install Nix from Binary Tarball Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Installs Nix by downloading, unpacking, and running an installer from a binary tarball. This method offers more control and is useful for specific system types or offline installations. Ensure you select the correct version and system type. ```console $ pushd $(mktemp -d) $ export VERSION=2.19.2 $ export SYSTEM=x86_64-linux $ curl -LO https://releases.nixos.org/nix/nix-$VERSION/nix-$VERSION-$SYSTEM.tar.xz $ tar xfj nix-$VERSION-$SYSTEM.tar.xz $ cd nix-$VERSION-$SYSTEM $ ./install $ popd ``` -------------------------------- ### Example `manifest.json` Structure Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/files/manifest.json.md This JSON structure illustrates the typical content of a `manifest.json` file, showing details of an installed package like its attribute path, original installation URI, and store path. ```json { "version": 1, "elements": [ { "active": true, "attrPath": "legacyPackages.x86_64-linux.zoom-us", "originalUrl": "flake:nixpkgs", "storePaths": [ "/nix/store/wbhg2ga8f3h87s9h5k0slxk0m81m4cxl-zoom-us-5.3.469451.0927" ], "uri": "github:NixOS/nixpkgs/13d0c311e3ae923a00f734b43fd1d35b47d8943a" }, … ] } ``` -------------------------------- ### Install Derivation from Nix Expression (All Outputs) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a derivation defined in a Nix expression file. By default, all outputs of the derivation are installed into the user profile. ```console nix-env --install --file example.nix ``` -------------------------------- ### Nix Bundler Configuration Example Source: https://github.com/nixos/nix/blob/master/src/nix/bundle.md An example of a Nix function defining bundlers for a specific system. ```nix bundlers.x86_64-linux = rec { identity = drv: drv; blender_2_79 = drv: self.packages.x86_64-linux.blender_2_79; default = identity; }; ``` -------------------------------- ### Install Nix in Development Shell Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/building.md Install Nix to the specified output directory and verify the version after building and testing. ```console [nix-shell]$ installPhase [nix-shell]$ nix --version ``` -------------------------------- ### Start Shell with youtube-dl Source: https://github.com/nixos/nix/blob/master/src/nix/shell.md Starts a shell environment that includes the `youtube-dl` package from the `nixpkgs` flake. Useful for immediately using the provided tool. ```console # nix shell nixpkgs#youtube-dl # youtube-dl --version 2020.11.01.1 ``` -------------------------------- ### Install from a store derivation path Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a package directly from its Nix store derivation path (e.g., created by `nix-instantiate`). ```console $ nix-env --install /nix/store/8la6y31fmm6i4wfmby6avly1wf718xnj-gcc-3.4.3.drv ``` -------------------------------- ### Install Nix using Binary Tarball Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.7.md Installs Nix by downloading and executing a bash script from a URL. This is a convenient way to install Nix on systems where binary packages are available. ```bash $ bash <(curl -L https://nixos.org/nix/install) ``` -------------------------------- ### Start nix-serve Daemon Source: https://github.com/nixos/nix/blob/master/doc/manual/source/package-management/binary-cache-substituter.md Starts the `nix-serve` daemon, making the Nix store available over HTTP on the specified port. Ensure the port is not already in use. ```console $ nix-serve -p 8080 ``` -------------------------------- ### Start Shell with Package Names Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-shell.md A shorter way to start an interactive shell with specified packages using the --packages flag. ```console $ nix-shell --packages sqlite xorg.libX11 [nix-shell]$ echo $NIX_LDFLAGS … -L/nix/store/j1zg5v…-sqlite-3.8.0.2/lib -L/nix/store/0gmcz9…-libX11-1.6.1/lib … ``` -------------------------------- ### Start Shell with GNU Hello from NixOS 20.03 Source: https://github.com/nixos/nix/blob/master/src/nix/shell.md Starts a shell environment providing the GNU Hello package from a specific older version of NixOS (20.03). ```console # nix shell nixpkgs/nixos-20.03#hello ``` -------------------------------- ### Install all derivations from a Nix file Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs all derivations defined within a specified Nix expression file. ```console $ nix-env --file ~/foo.nix --install '.*' ``` -------------------------------- ### Install Built Store Path Directly Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-0.8.md Install a package by directly referencing an already built store path. ```bash $ nix-env -i /nix/store/hsyj5pbn0d9i...-aterm-2.3.1 ``` -------------------------------- ### Start Nix Daemon Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/multi-user.md Starts the Nix daemon. This command should be added to system boot scripts to ensure the daemon runs on startup. ```bash $ nix-daemon ``` -------------------------------- ### Bundle Hello World Source: https://github.com/nixos/nix/blob/master/src/nix/bundle.md Bundles the 'nixpkgs#hello' installable and executes it. ```console # nix bundle nixpkgs#hello # ./hello Hello, world! ``` -------------------------------- ### Install Package from Tarball URL with nix-env Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.9.md Use nix-env to install a package by specifying the URL of a tarball containing Nix expressions. This example installs Firefox from a specific Nixpkgs channel archive. ```bash $ nix-env -f https://github.com/NixOS/nixpkgs-channels/archive/nixos-14.12.tar.gz -iA firefox ``` -------------------------------- ### Basic Lookup Path Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/language/constructs/lookup-path.md Demonstrates a simple lookup path `` which resolves to a specific path. ```nix ``` -------------------------------- ### Install package by name (arbitrary version) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a package by its general name, allowing Nix to select a version (often the latest available). ```console $ nix-env --install gcc installing `gcc-3.3.2' ``` -------------------------------- ### Getting JSON Output for Installed Packages Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.7.md Demonstrates the --json flag for `nix-env -q` to obtain a JSON representation of installed or available packages. ```bash nix-env -q --json ``` -------------------------------- ### Next Steps After Project Initialization Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/cli-guideline.md Provides guidance on the usual development workflow with Nix after initializing a new project. ```shell $ nix init --template=template#python Initializing project `template#python` in `/home/USER/dev/new-project` Next steps |> nix develop -- to enter development environment |> nix build -- to build your project ``` -------------------------------- ### Install Nix on Linux Multi-User Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/upgrading.md Installs the Nix package and cacert on a Linux system with a multi-user setup using the unstable channel. Requires root privileges. ```console $ sudo su # nix-env --install --file '' --attr nix cacert -I nixpkgs=channel:nixpkgs-unstable # systemctl daemon-reload # systemctl restart nix-daemon ``` -------------------------------- ### Bundle Hello with Docker Image Bundler Source: https://github.com/nixos/nix/blob/master/src/nix/bundle.md Bundles 'nixpkgs#hello' using the 'toDockerImage' bundler, loads it into Docker, and runs it. ```console # nix bundle --bundler github:NixOS/bundlers#toDockerImage nixpkgs#hello # docker load < hello-2.10.tar.gz # docker run hello-2.10:latest hello Hello, world! ``` -------------------------------- ### Build and Print Output Paths Source: https://github.com/nixos/nix/blob/master/src/nix/build.md Builds the 'hello' package from 'nixpkgs' and prints the resulting store path to standard output. Useful for scripting and automation. ```console # nix build nixpkgs#hello --print-out-paths /nix/store/10l19qifk7hjjq47px8m2prqk1gv4isy-hello-2.10 ``` -------------------------------- ### Path Flake Reference Examples Source: https://github.com/nixos/nix/blob/master/src/nix/flake.md Examples of path-based flake references for local directories. The 'path:' prefix is optional for absolute paths not in a git repository, and for relative paths starting with './'. ```nix path:/home/user/sub/dir ``` ```nix /home/user/sub/dir ``` ```nix path:sub/dir ``` ```nix ./sub/dir ``` ```nix path:../parent ``` -------------------------------- ### Simple Build Trace Entry Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/protocols/json/build-trace-entry.md A basic example of a build trace entry in JSON format. This serves as a fundamental structure for recording build events. ```json { "outputs": { "out": { "path": "/nix/store/s4z4f7z4f7z4f7z4f7z4f7z4f7z4f7z4-hello-2.10", "hashAlgo": "sha256", "hash": "1111111111111111111111111111111111111111111111111111" } }, "inputDrvs": { "/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-hello-2.10.drv": "/nix/store/s4z4f7z4f7z4f7z4f7z4f7z4f7z4f7z4-hello-2.10" }, "logs": {}, "time": 1678886400, "duration": 1000, "successful": true, "name": "hello-2.10", "system": "x86_64-linux", "path": "/nix/store/s4z4f7z4f7z4f7z4f7z4f7z4f7z4f7z4-hello-2.10" } ``` -------------------------------- ### Copying a Store Path from a Remote Machine and Running It Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-copy-closure.md This example shows how to retrieve a specific store path (GNU Hello's output path) from a remote machine ('itchy.example.org' as user 'alice') to the local machine. After copying, it demonstrates how to execute the program located at the retrieved store path. ```shell $ storePath="$(nix-instantiate --eval --raw '' -I nixpkgs=channel:nixpkgs-unstable -A hello.outPath)" $ nix-copy-closure --from alice@itchy.example.org "$storePath" $ "$storePath"/bin/hello Hello, world! ``` -------------------------------- ### Install Nix on macOS Multi-User Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/upgrading.md Installs the Nix package and cacert on a macOS system with a multi-user setup using the unstable channel. Requires root privileges and involves managing the Nix daemon service. ```console $ sudo nix-env --install --file '' --attr nix cacert -I nixpkgs=channel:nixpkgs-unstable $ sudo launchctl remove org.nixos.nix-daemon $ sudo launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist ``` -------------------------------- ### Example Include Path in Nix Store Source: https://github.com/nixos/nix/blob/master/doc/manual/source/introduction.md Shows how include paths for packages are stored in the Nix store, demonstrating explicit dependency management. ```text /nix/store/5lbfaxb722zp…-openssl-0.9.8d/include ``` -------------------------------- ### Nix Block Comment Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/language/syntax.md Block comments start with '/*' and end with '*/', allowing multi-line comments. They cannot be nested. ```nix /* Block comments can span multiple lines. */ "hello" ``` -------------------------------- ### Nix Inline Comment Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/language/syntax.md Inline comments start with '#' and extend to the end of the line. They can be used to add brief explanations within code. ```nix # A number 2 # Equals 1 + 1 ``` -------------------------------- ### Setting up a Shell Environment with Specified Packages Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.7.md Shows how to use the --packages flag with nix-shell to create a shell environment that includes specific packages from Nixpkgs. ```bash $ nix-shell -p sqlite xorg.libX11 hello ``` -------------------------------- ### Nix Profile Version Contents Example Source: https://github.com/nixos/nix/blob/master/src/nix/profiles.md Shows the contents of a specific Nix profile version, detailing the symlinks to installed packages in the bin and share directories. ```console $ ll -R ~eelco/.local/state/nix/profiles/profile-7-link/ /home/eelco/.local/state/nix/profiles/profile-7-link/: total 20 dr-xr-xr-x 2 root root 4096 Jan 1 1970 bin -r--r--r-- 2 root root 1402 Jan 1 1970 manifest.nix dr-xr-xr-x 4 root root 4096 Jan 1 1970 share /home/eelco/.local/state/nix/profiles/profile-7-link/bin: total 20 lrwxrwxrwx 5 root root 79 Jan 1 1970 chromium -> /nix/store/cyxny9d1zjb9l9103fr6j6kavp3bqjxf-chromium-86.0.4240.111/bin/chromium lrwxrwxrwx 7 root root 87 Jan 1 1970 spotify -> /nix/store/w9182874m1bl56smps3m5zjj36jhp3rn-spotify-1.1.26.501.gbe11e53b-15/bin/spotify lrwxrwxrwx 3 root root 79 Jan 1 1970 zoom-us -> /nix/store/wbhg2ga8f3h87s9h5k0slxk0m81m4cxl-zoom-us-5.3.469451.0927/bin/zoom-us /home/eelco/.local/state/nix/profiles/profile-7-link/share/applications: total 12 lrwxrwxrwx 4 root root 120 Jan 1 1970 chromium-browser.desktop -> /nix/store/sqzyx2l85i6j2a77pnyvglh3bvzwmjjp-chromium-unwrapped-86.0.4240.111/share/applications/chromium-browser.desktop lrwxrwxrwx 7 root root 110 Jan 1 1970 spotify.desktop -> /nix/store/w9182874m1bl56smps3m5zjj36jhp3rn-spotify-1.1.26.501.gbe11e53b-15/share/applications/spotify.desktop lrwxrwxrwx 3 root root 107 Jan 1 1970 us.zoom.Zoom.desktop -> /nix/store/wbhg2ga8f3h87s9h5k0slxk0m81m4cxl-zoom-us-5.3.469451.0927/share/applications/us.zoom.Zoom.desktop ``` -------------------------------- ### New Build Trace Key Example Source: https://github.com/nixos/nix/blob/master/doc/manual/rl-next/build-trace-rework.md Shows the new format for build trace keys, using the regular derivation store path and output name. ```text /nix/store/abc...-foo.drv^out ``` -------------------------------- ### Nix Build Command Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/cli-guideline.md Demonstrates the expected output for a Nix build command, showing download progress and a success message. This output clearly indicates command completion. ```shell $ nix build Downloaded python3.8-poetry 1.2.3 in 5.3 seconds Downloaded python3.8-requests 1.2.3 in 5.3 seconds ... Success! You have successfully built my-project. $ ``` -------------------------------- ### Defining a shellHook in Nix Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-shell.md This example shows how to define a shellHook in a Nix derivation. The hook runs after environment setup and can be used for initialization tasks like setting environment variables. ```nix shellHook = '' echo "Hello shell" export SOME_API_TOKEN="$(cat ~/.config/some-app/api-token)" ''; ``` -------------------------------- ### Download a file and get SHA-512 hash Source: https://github.com/nixos/nix/blob/master/src/nix/store-prefetch-file.md This example demonstrates how to download a file, specify SHA-512 as the hash type, and extract only the hash value using 'jq'. This is useful for scripting and automation. ```console # nix store prefetch-file --json --hash-type sha512 \ https://releases.nixos.org/nix/nix-2.3.10/nix-2.3.10.tar.xz \ | jq -r .hash sha512-6XJxfym0TNH9knxeH4ZOvns6wElFy3uahunl2hJgovACCMEMXSy42s69zWVyGJALXTI+86tpDJGlIcAySEKBbA== ``` -------------------------------- ### Build Nix Manual from Scratch Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/documentation.md Use this command to perform a full build of the Nix manual. The output can be found in the specified path. ```console nix-build -E '(import ./.).packages.${builtins.currentSystem}.nix.doc' ``` -------------------------------- ### Use Tarball URL in Nix Path Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.9.md Specify a tarball URL in the Nix path (NIX_PATH or -I) to use a specific version of Nixpkgs. This example starts a shell with the Pan package from a specific Nixpkgs commit. ```bash $ nix-shell -p pan -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/8a3eea054838b55aca962c3fbde9c83c102b8bf2.tar.gz ``` -------------------------------- ### Modifying package meta attributes with nix-env --set-flag Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-0.11.md Use `nix-env --set-flag` to modify meta attributes of installed packages. This is useful for adjusting `meta.priority`, `meta.keep`, or `meta.active` to control package behavior and environment setup. ```bash nix-env --set-flag meta.keep true ``` ```bash nix-env --set-flag meta.active false ``` -------------------------------- ### Nix Store Path Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/store/store-path.md Illustrates the components of a typical Nix store path, showing the store directory, digest, and name. ```text /nix/store/q06x3jll2yfzckz2bzqak089p43ixkkq-firefox-33.1 |--------| |------------------------------| |----------| store directory digest name ``` -------------------------------- ### Using the basic fetchurl function Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-1.1.md This snippet demonstrates how to use the basic `fetchurl` function for bootstrapping purposes. It requires the URL and a SHA256 hash of the content. ```nix import { url = url; sha256 = "hash"; } ``` -------------------------------- ### Uninstall Nix with Rust Installer Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.34.md Use this command to uninstall Nix if it was installed using the new Rust-based installer. This will remove the entire Nix installation. ```bash /nix/nix-installer uninstall ``` -------------------------------- ### Markdown Admonition Example (Example) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/documentation.md Illustrates the markdown format for highlighting code examples within documentation using the 'Example' admonition type and a console code block. ```markdown > **Example** > > ```console > $ nix --version > ``` ``` -------------------------------- ### Compile and Run Nix Evaluator Example Source: https://github.com/nixos/nix/blob/master/src/external-api-docs/README.md Demonstrates how to compile the C code using pkg-config and execute the program to display the Nix version. ```ShellSession $ gcc main.c $(pkg-config nix-expr-c --libs --cflags) -o main $ ./main Nix version: 2.17 ``` -------------------------------- ### Install Latest Nix Binary Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Installs the latest stable version of Nix using the default installation script. This is the recommended method for most users. ```console $ curl -L https://nixos.org/nix/install | sh ``` -------------------------------- ### Simple Nix Flake Example Source: https://github.com/nixos/nix/blob/master/src/nix/flake.md This example demonstrates a basic flake.nix file that defines a single package. It depends on the Nixpkgs flake and uses `stdenv.mkDerivation` to build a 'hello' program. ```nix { description = "A flake for building Hello World"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-20.03"; outputs = { self, nixpkgs }: { packages.x86_64-linux.default = # Notice the reference to nixpkgs here. with import nixpkgs { system = "x86_64-linux"; }; stdenv.mkDerivation { name = "hello"; src = self; buildPhase = "gcc -o hello ./hello.c"; installPhase = "mkdir -p $out/bin; install -t $out/bin hello"; }; }; } ``` -------------------------------- ### Install Nix (Single-user) Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/index.md Install Nix using the single-user option, suitable for systems without systemd. Note that installing as root is not supported by this script. ```bash $ curl -L https://nixos.org/nix/install | sh -s -- --no-daemon ``` -------------------------------- ### Nix Store Derivation Path Example Source: https://github.com/nixos/nix/blob/master/src/nix/nix.md Represents a derivation file, which defines how to build a package. ```text /nix/store/p7gp6lxdg32h4ka1q398wd9r2zkbbz2v-hello-2.10.drv ``` -------------------------------- ### Generate Install Command with Artifact Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/testing.md Use this command template to generate an install command by providing the path to the unpacked installer artifact. This is useful for manual testing after CI runs. ```bash sh /install --tarball-url-prefix file:// ``` -------------------------------- ### Start Shell with Overridden Package Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-shell.md Demonstrates using the --packages flag with a Nix expression override, allowing for customized package configurations. ```console $ nix-shell --packages sqlite 'git.override { withManual = false; }' ``` -------------------------------- ### Instantiate and Build a Nix Derivation Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-instantiate.md Instantiates a Nix derivation from a file and then builds it using nix-store. Shows the resulting store path. ```console $ nix-instantiate test.nix (instantiate) /nix/store/cigxbmvy6dzix98dxxh9b6shg7ar5bvs-perl-BerkeleyDB-0.26.drv $ nix-store --realise $(nix-instantiate test.nix) (build) ... /nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26 (output path) $ ls -l /nix/store/qhqk4n8ci095g3sdp93x7rgwyh9rdvgk-perl-BerkeleyDB-0.26 dr-xr-xr-x 2 eelco users 4096 1970-01-01 01:00 lib ... ``` -------------------------------- ### Install package by derivation name Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a specific version of a package using its derivation name. Note that if `--preserve-installed` is not used, previously installed versions may be removed. ```console $ nix-env --install gcc-3.3.2 installing `gcc-3.3.2' uninstalling `gcc-3.1' ``` -------------------------------- ### Nix Profile Filesystem Layout Example Source: https://github.com/nixos/nix/blob/master/src/nix/profiles.md Illustrates the symlink structure of Nix profiles, showing how a profile link points to a versioned link, which in turn points to a Nix store path. ```console $ ls -l ~alice/.local/state/nix/profiles/profile* lrwxrwxrwx 1 alice users 14 Nov 25 14:35 /home/alice/.local/state/nix/profiles/profile -> profile-7-link lrwxrwxrwx 1 alice users 51 Oct 28 16:18 /home/alice/.local/state/nix/profiles/profile-5-link -> /nix/store/q69xad13ghpf7ir87h0b2gd28lafjj1j-profile lrwxrwxrwx 1 alice users 51 Oct 29 13:20 /home/alice/.local/state/nix/profiles/profile-6-link -> /nix/store/6bvhpysd7vwz7k3b0pndn7ifi5xr32dg-profile lrwxrwxrwx 1 alice users 51 Nov 25 14:35 /home/alice/.local/state/nix/profiles/profile-7-link -> /nix/store/mp0x6xnsg0b8qhswy6riqvimai4gm677-profile ``` -------------------------------- ### Force Multi-User Nix Installation Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Explicitly instructs the installer to perform a multi-user installation, which is recommended for Linux systems with systemd and without SELinux, and for macOS. The script handles sudo invocation. ```console $ bash <(curl -L https://nixos.org/nix/install) --daemon ``` -------------------------------- ### Install Pinned Nix Version Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Installs a specific version of Nix by setting the VERSION environment variable and then running the install script from a version-specific URL. Ensure the specified version is available on releases.nixos.org. ```console $ export VERSION=2.19.2 $ curl -L https://releases.nixos.org/nix/nix-$VERSION/install | sh ``` -------------------------------- ### Install a Package using nix-env Source: https://github.com/nixos/nix/blob/master/doc/manual/source/package-management/profiles.md Installs a specific package from Nix expressions into the user environment. ```console $ nix-env --install --attr nixpkgs.subversion ``` -------------------------------- ### Viewing nix-env Operation Help Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env.md Demonstrates how to view help for specific nix-env operations, either via man pages or the --help flag. ```bash man nix-env-install ``` ```bash nix-env --help --install ``` -------------------------------- ### Shell Completion Example Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/cli-guideline.md Demonstrates context-aware shell completion for Nix commands, suggesting relevant flakes based on user input. ```shell $ nix build n ``` -------------------------------- ### Manually Create /nix for Single-User Install Source: https://github.com/nixos/nix/blob/master/doc/manual/source/installation/installing-binary.md Provides manual steps to create the /nix directory and set ownership for a single-user installation if sudo is not available. This is a fallback for environments where sudo cannot be used. ```console $ su root # mkdir /nix # chown alice /nix ``` -------------------------------- ### Mounting Nix Store with Bind Mount Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/env-common.md Example of how to use bind mount points on Linux to place the Nix store on a different file system. This is a recommended alternative to symlinking the Nix store directory. ```bash $ mkdir /nix $ mount -o bind /mnt/otherdisk/nix /nix ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/nixos/nix/blob/master/HACKING.md Install pre-commit hooks to automatically run formatters before each commit. This uses cachix/git-hooks.nix. ```shell pre-commit-hooks-install ``` -------------------------------- ### Install GDB Debugger Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/debugging.md Install the GNU Debugger (GDB) within the development shell for debugging Nix. ```console [nix-shell]$ nix-shell -p gdb ``` -------------------------------- ### Configure and Build Nix Source: https://github.com/nixos/nix/blob/master/HACKING.md Set up build variables and execute the configure and build phases within the development shell. The output will be placed in $(pwd)/outputs/out. ```console [nix-shell]$ out="$(pwd)/outputs/out" dev=$out debug=$out mesonFlags+=" --prefix=${out}" [nix-shell]$ dontAddPrefix=1 configurePhase [nix-shell]$ buildPhase ``` -------------------------------- ### Build Package Using Nix Path Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.4.md Alternatively, use the '-f ""' option with the 'hello' package if 'nixpkgs' is in your NIX_PATH environment variable. ```console nix build -f '' hello ``` -------------------------------- ### Example Protocol: Store Paths Source: https://github.com/nixos/nix/blob/master/src/nix/unix/store-roots-daemon.md This example illustrates the protocol used by the roots daemon. The server sends zero or more store paths, each terminated by a NUL character (`\0`). The example shows these paths with newlines for clarity, but they are NUL-delimited in actual communication. ```text /nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1 /nix/store/fvpr7x8l3illdnziggvkhdpf6vikg65w-git-2.44.0 ``` -------------------------------- ### Enter Nix Build Environment Source: https://github.com/nixos/nix/blob/master/doc/manual/source/introduction.md Use `nix-shell` to automatically set up the build environment for a package. This command builds or downloads dependencies and starts a shell with necessary environment variables set. ```console $ nix-shell '' --attr pan ``` -------------------------------- ### Print Store Path of an Installable Source: https://github.com/nixos/nix/blob/master/src/nix/path-info.md Use this to display the Nix store path for a given installable, such as a package from nixpkgs. ```console # nix path-info nixpkgs#hello /nix/store/10l19qifk7hjjq47px8m2prqk1gv4isy-hello-2.10 ``` -------------------------------- ### Load Nix Expressions from File Source: https://github.com/nixos/nix/blob/master/src/nix/repl.md Start the Nix REPL with a specific Nix expression file loaded. ```console nix repl --file example.nix Loading Installable ''... Added 3 variables. ``` -------------------------------- ### Install Multiple Packages using nix-env Source: https://github.com/nixos/nix/blob/master/doc/manual/source/package-management/profiles.md Installs multiple packages, creating a new generation of the user environment. ```console $ nix-env --install --attr nixpkgs.subversion nixpkgs.firefox ``` -------------------------------- ### Package Version and Size Change Example Source: https://github.com/nixos/nix/blob/master/src/nix/diff-closures.md Illustrates the output format for a package with a version change and a size difference exceeding the threshold. ```console dolphin: 20.08.1 → 20.08.2, +13.9 KiB ``` -------------------------------- ### Build and Run Package from Nixpkgs Flake Source: https://github.com/nixos/nix/blob/master/src/nix/build.md Builds the 'hello' package from the 'nixpkgs' flake and then runs the resulting executable. Demonstrates building a specific package from a remote flake. ```console # nix build nixpkgs#hello # ./result/bin/hello ``` -------------------------------- ### Build C API Documentation Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/documentation.md Builds the C API documentation using `nix build` and opens the generated HTML files. ```console $ nix build .#hydraJobs.external-api-docs $ xdg-open ./result/share/doc/nix/external-api/html/index.html ``` -------------------------------- ### Install LLDB Debugger on macOS Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/debugging.md Install the LLDB debugger within the development shell on macOS for debugging Nix. ```console [nix-shell]$ nix-shell -p lldb ``` -------------------------------- ### Indirect Flake Reference Examples Source: https://github.com/nixos/nix/blob/master/src/nix/flake.md Examples of indirect flake references, which are symbolic references looked up in flake registries. ```nix nixpkgs ``` ```nix nixpkgs/nixos-unstable ``` ```nix nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293 ``` ```nix nixpkgs/nixos-unstable/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293 ``` ```nix sub/dir ``` -------------------------------- ### Install Store Derivation Directly Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-0.8.md Install a package by directly referencing its derivation file, bypassing the Nix expression language. ```bash $ nix-env -i /nix/store/z58v41v21xd3...-aterm-2.3.1.drv ``` -------------------------------- ### Show all paths from Thunderbird to libX11 Source: https://github.com/nixos/nix/blob/master/src/nix/why-depends.md Use the `--all` flag to list all dependency paths and the files containing references from 'thunderbird' to 'xorg.libX11'. This can reveal multiple reasons for a dependency. ```console # nix why-depends --all nixpkgs#thunderbird nixpkgs#xorg.libX11 /nix/store/0my2p7psgdzqc5pq6dyl4ld9w6g0np58-thunderbird-78.5.1 ├───lib/thunderbird/libxul.so: …6wrw-libxcb-1.14/lib:/nix/store/jmwiq1bb3n47a0css8b1q7lhgf7416k5-libX11-1.7.0/lib:/nix/store/ssf… │ → /nix/store/jmwiq1bb3n47a0css8b1q7lhgf7416k5-libX11-1.7.0 ├───lib/thunderbird/libxul.so: …pxyc-libXt-1.2.0/lib:/nix/store/l1sv43bafhkf2iikmdw9y62aybjdhcmm-libXdamage-1.1.5/lib:/nix/store… │ → /nix/store/l1sv43bafhkf2iikmdw9y62aybjdhcmm-libXdamage-1.1.5 │ ├───lib/libXdamage.so.1.1.0: …-libXfixes-5.0.3/lib:/nix/store/jmwiq1bb3n47a0css8b1q7lhgf7416k5-libX11-1.7.0/lib:/nix/store/9l0… │ │ → /nix/store/jmwiq1bb3n47a0css8b1q7lhgf7416k5-libX11-1.7.0 … ``` -------------------------------- ### Running a Package with a Custom Store Path Source: https://github.com/nixos/nix/blob/master/doc/manual/source/release-notes/rl-2.0.md This command downloads or builds the GNU Hello package into a specified local store path and then runs the `hello` executable within a mount namespace where the custom store is mounted onto `/nix/store`. ```bash nix run --store ~/my-nix nixpkgs.hello -c hello --greeting 'Hi everybody!' ``` -------------------------------- ### Install package from a specific channel archive Source: https://github.com/nixos/nix/blob/master/doc/manual/source/command-ref/nix-env/install.md Installs a package from a specific revision of a channel, identified by a URL to a tarball archive. ```console $ nix-env --file https://github.com/NixOS/nixpkgs/archive/nixos-14.12.tar.gz --install --attr firefox ``` -------------------------------- ### Generate Installer Script Source: https://github.com/nixos/nix/blob/master/doc/manual/source/development/testing.md This command generates the installer tarball and script for a specified system. Replace with the target system architecture. ```bash nix build .#hydraJobs.installerScriptForGHA. ```