{"id":13837911,"url":"https://github.com/colemickens/nixos-flake-example","last_synced_at":"2025-03-16T14:31:12.322Z","repository":{"id":40556267,"uuid":"307019973","full_name":"colemickens/nixos-flake-example","owner":"colemickens","description":"This is a demo NixOS config, with optional flakes support. Along with notes on why flakes is useful and worth adopting.","archived":false,"fork":false,"pushed_at":"2021-05-31T00:34:14.000Z","size":50,"stargazers_count":178,"open_issues_count":2,"forks_count":19,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-02-27T10:39:53.465Z","etag":null,"topics":["evangelism","flakes","nix"],"latest_commit_sha":null,"homepage":"","language":"Nix","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/colemickens.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-25T03:55:40.000Z","updated_at":"2025-02-06T00:14:41.000Z","dependencies_parsed_at":"2022-09-02T18:30:33.124Z","dependency_job_id":null,"html_url":"https://github.com/colemickens/nixos-flake-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colemickens%2Fnixos-flake-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colemickens%2Fnixos-flake-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colemickens%2Fnixos-flake-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colemickens%2Fnixos-flake-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/colemickens","download_url":"https://codeload.github.com/colemickens/nixos-flake-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243818194,"owners_count":20352629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evangelism","flakes","nix"],"created_at":"2024-08-04T15:01:30.295Z","updated_at":"2025-03-16T14:31:11.763Z","avatar_url":"https://github.com/colemickens.png","language":"Nix","funding_links":[],"categories":["Nix"],"sub_categories":[],"readme":"# nixos-flake-example\n\n## warning\n\n**WARNING**: You should understand that:\n\n- there is currently no path to flakes being stable\n- we can't even experiment with flakes alternatives without committing to pure-eval as a stepping stone\n- the people that CARE about the problems solved by flakes seem to care about solving this problem\n- the people using impurity everywhere don't seem very motivated to dive into this problem-space\n- as a result, the tooling is completely fractured and stagnant\n- besides some UX fixes, bugfixes, and other feature-work in Nix, this has more or less been the case for years\n- I'm tired of seeing (often self-proclaimed, repeated-from-others) FUD by non-flakes users\n \n  Anyway, now you know. Proceed at your own caution.\n\n## overview\n\n**NOTE**: [nixflk](https://github.com/nrdxp/nixflk) is a better example repo for a full \nNixOS config layout, this repo is mostly to provide more context+examples around flakes, \nand to show that you can produce the same EXACT system with flakes as with nix-build, \nif you know what to do.\n\nThis readme starts out with an attempt to explain and justify flakes. It also contains\nsome examples of `nix` cli flakes syntax and tips for adopting flakes in your project.\n\nFinally, [at the end of the readme](#example-nixos-config-with-optional-flake-support)\nis an example NixOS config with a supporting `flake.nix`,\nand instructions to build it with *and without* flakes support at the same time.\n\n- [Overview of Flakes (and why you want it)](#overview-of-flakes-and-why-you-want-it)\n- [Important Related Reading](#important-related-reading)\n- [Nix CLI - Flakes Usage](#nix-cli---flakes-usage)\n  - [Useful Commands and Examples](#useful-commands-and-examples)\n    - [nixos-rebuild](#nixos-rebuild)\n    - [nix build](#nix-build)\n    - [nix flake](#nix-flake)\n  - [Auto-coercion examples](#auto-coercion-examples)\n- [Tips for Porting to Flakes](#tips-for-porting-to-flakes)\n- [Example NixOS Config with optional Flake support](#example-nixos-config-with-optional-flake-support)\n\n## Overview of Flakes (and why you want it)\n\nFlakes is a few things:\n* `flake.nix`: a Nix file, with a specific structure to describe inputs and outputs for a Nix project\n  * See [NixOS Wiki - Flakes - Input Schema](https://nixos.wiki/wiki/Flakes#Input_schema) for flake input examples\n  * See [NixOS Wiki - Flakes - Output Schema](https://nixos.wiki/wiki/Flakes#Input_schema) for flake output examples\n* `flake.lock`: a manifest that \"locks\" inputs and records the exact versions in use\n* CLI support for flake-related features\n* pure (by default) evaluations\n\nThis ultimately enables:\n* properly hermetic builds\n* fully reproducable and portable Nix projects\n* faster Nix operations due to evaluation caching enabled by pure evaluations)\n\nThis removes the need for:\n* using `niv` or other tooling to lock dependencies\n* manually documenting or scripting to ensure `NIX_PATH` is set consistently for your team\n* the need for the *\"the impure eval tree of sorrow\"* that comes with all of today's Nix impurities\n\n## Important Related Reading\n\n* [NixOS Wiki - Flakes](https://nixos.wiki/wiki/Flakes)\n  * a somewhat haphazard collection of factoids/snippets related to flakes\n  * particularly look at: **[Flake Schema](https://nixos.wiki/wiki/Flakes#Flake_schema)**, and it's two sections: **[Input Schema](https://nixos.wiki/wiki/Flakes#Input_schema)**, **[Output Schema](https://nixos.wiki/wiki/Flakes#Output_schema)**\n* [Tweag - NixOS flakes](https://www.tweag.io/blog/2020-07-31-nixos-flakes/)\n  * this article describes how to enable flake support in `nix` and `nix-daemon`\n  * reading this article is a **pre-requisite**\n  * this README.md assumes you've enabled flakes system-wide\n  * omit using `boot.isContainer = true;` on `configuration.nix` (as the article suggests) if you want to use `nixos-rebuild` rather than `nixos-container` \n\n## Nix CLI - Flakes Usage\n\nNix is in flakes mode when:\n* `--flake` is used with the `nixos-rebuild` command\n* or, when `nix build` is used with an argument like `'.#something'`  (the hash symbol separates the flake source from the attribute to build)\n\nWhen in this mode:\n* Nix flake commands will implicitly take a directory path, it expects a `flake.nix` inside\n* when you see: `nix build '.#something'`, the `.` means current directory, and `#something` means to build the `something` output attribute\n\n### Useful Commands and Examples\n#### nixos-rebuild\n* `nixos-rebuild build --flake '.#'`\n  * looks for `flake.nix` in `.` (current dir)\n  * since it's `nixos-rebuild`, it automatically tries to build:\n    * `#nixosConfigurations.{hostname}.config.system.build.toplevel`\n* `nixos-rebuild build --flake '/code/nixos-config#mysystem'`\n  * looks for `flake.nix` in `/code/nixos-config`\n  * since it's `nixos-rebuild`, it automatically tries to build:\n    * `#nixosConfigurations.mysystem.config.system.build.toplevel`\n    * (note that this time we specifically asked, and got to build the `mysystem` config)\n#### nix build\n* `nix build 'github:colemickens/nixpkgs-wayland#obs-studio'`\n  * looks for `flake.nix`  in (a checkout of `github.com/colemickens/nixpkgs-wayland`)\n  * builds and run the first attribute found:\n    * `#obs-studio`\n    * `#packages.{currentSystem}.obs-studio`\n    * TODO: finish fleshing out this list\n#### nix flake\n* `nix flake update --recreate-lock-file`\n  * updates all inputs and recreating `flake.lock`\n* `nix flake update --update-input nixpkgs`\n  * updates a single input to latest and recording it in `flake.lock`\n\n### Auto-coercion examples\n\nNix CLI will try to be ... smart and auto-coerce some output attribute paths for you.\n\n* `nix build '/some/path#obs-studio'`:\n  * builds and run the first attribute found:\n    * `/some/path#obs-studio`\n    * `/some/path#packages.x86_64-linux.obs-studio`\n    * `/some/path#legacyPackages.x86_64-linux.obs-studio`\n    * TODO: finish fleshing out this list\n    * TODO: not sure about search order, presumably the bare one would be priority\n\n## Tips for Porting to Flakes\n\n**Remove Impurities** - Since nix flakes does a 'pure' build by default,\n  * `NIX_PATH` is ignored\n  * `\u003cnixpkgs\u003e` imports do not work, and explicitly error\n  * local user nixpkgs config (`~/.config/{nix,nixpkgs}`) are ignore\n  * unpinned imports (aka, `fetchTarball` without a pinned `rev`+`sha256`) are forbidden\n\nTo fix these:\n  * specify all remote imports in `flake.nix` instead of using `fetchTarball`\n    * the config in this repo shows an example of using the overlay from\n      `nixpkgs-wayland`.\n    * TODO: investigate `getFlake` vs  passing `inputs` in `specialArgs`\n\n## Example NixOS Config with optional Flake support\n\nConsider the nixos configuration in this repo:\n* [./configuration.nix](./configuration.nix)\n* [./hardware-configuration.nix](./hardware-configuration.nix)\n\nThese represent an example, minimal NixOS system configuration.\n\nThe easiest way to build it, without cloning this repo:\n```\nnix build 'github:colemickens/nixos-flake-example#nixosConfigurations.mysystem.config.system.build.toplevel'\n```\n\nLet's prove that we can build this config, with and without flakes:\n\n* Using `nixos-rebuild`:\n    ```shell\n    # with flakes\n    unset NIX_PATH\n    nixos-rebuild build --flake '.#mysystem'\n    readlink -f ./result\n    /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n\n    # !! for this next step, match the git SHA1 to what the flake.lock uses\n    #    otherwise you'll have a hash mismatch due to different nixpkgs\n\n    # without flakes\n    export NIX_PATH=nixpkgs=https://github.com/nixos/nixpkgs/archive/007126eef72271480cb7670e19e501a1ad2c1ff2.tar.gz:nixos-config=/home/cole/code/nixos-flake-example/configuration.nix\n    nixos-rebuild build\n    readlink -f ./result\n    /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n    ```\n\n* Using `nix build`:\n    ```shell\n    # with flakes\n    unset NIX_PATH\n    nix build '.#nixosConfigurations.mysystem.config.system.build.toplevel\n    readlink -f ./result\n    /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n\n    # without flakes\n    export NIX_PATH=nixpkgs=https://github.com/nixos/nixpkgs/archive/007126eef72271480cb7670e19e501a1ad2c1ff2.tar.gz:nixos-config=/home/cole/code/nixos-flake-example/configuration.nix\n    nix-build '\u003cnixos/nixpkgs\u003e' -A config.system.build.toplevel\n    readlink -f ./result\n    /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n    ```\n\n* The `./check.sh` script automates this process:\n\n    ```shell\n    cole@slynux ~/code/nixos-flake-example master* 7s\n    ❯ ./check.sh     \n\n    :: Updating the 'nixpkgs' input in flake.nix\n    + nix flake update --update-input nixpkgs\n    + set +x\n\n    :: Using 'nixos-rebuild' to build the 'mysystem' toplevel\n    + nixos-rebuild build --flake '.#mysystem'\n    warning: Git tree '/home/cole/code/nixos-flake-example' is dirty\n    building the system configuration...\n    warning: Git tree '/home/cole/code/nixos-flake-example' is dirty\n    + set +x\n\n    :: Using rev=007126eef72271480cb7670e19e501a1ad2c1ff2 for \u003cnixpkgs\u003e (extracted from flake.nix)\n\n    :: Setting NIX_PATH to the same values flakes is using\n    + NIX_PATH=nixpkgs=https://github.com/nixos/nixpkgs/archive/007126eef72271480cb7670e19e501a1ad2c1ff2.tar.gz:nixos-config=/home/cole/code/nixos-flake-example/configuration.nix\n    + nix-build '\u003cnixpkgs/nixos\u003e' -A config.system.build.toplevel\n    /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n    + set +x\n\n    flake: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n    clssc: /nix/store/gg1jhmzqndqa0rfnwfdbnzrn8f74ckr6-nixos-system-mysystem-21.03pre-git\n    ```\n\n# Flake Feedback/Ponderings\n\n- Is the hash tag syntax really worth it?\n  - For example, is:\n    - `nix build 'github:colemickens/nixpkgs-wayland#obs-studio'`\n  - really better than:\n    - `nix build --flake 'github:colemickens/nixpkgs-wayland' 'obs-studio'` ?\n\n- Are the auto-coercion rules for attribute paths worth it?\n  They definitely add some mental overhead...\n\n- \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolemickens%2Fnixos-flake-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcolemickens%2Fnixos-flake-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolemickens%2Fnixos-flake-example/lists"}