Skip to content

Latest commit

 

History

History
177 lines (137 loc) · 4.06 KB

30-flakes-intro.org

File metadata and controls

177 lines (137 loc) · 4.06 KB

Flakes ❄️ & pinning sources 📌

How to manage Nix dependencies?


nix-channel
impure; nixpkgs may change at any time
Local nixpkgs checkout
this can be quite unreliable. Alternatively you can include a git-submodule or git-subtree in your configuration repository.
Niv
Similar to flakes in a lot of ways, but much smaller in scope. Developed by community members https://github.com/nmattia/niv
Flakes
The hot new shit that everyone is talking about

Subtree & Submodule


  • Lock a specific configuration state to a particular nixpkgs version.
  • Rolling back configuration also rolls back nixpkgs automatically.
  • Subtrees can be heavy on a repository, because the entire history is merged
    • They do potentially make upstream contributions easier though.

Niv


  • Developed by Nix community since 2019
  • Specify dependencies via sources.json file
{
  "nixpkgs": {
      "branch": "nixos-unstable",
      "description": "Nix Packages collection",
      "homepage": "",
      "owner": "NixOS",
      "repo": "nixpkgs",
      "rev": "79d3ca08920364759c63fd3eb562e99c0c17044a",
      "sha256": "1zz72k161yl9dxs5nxgy5p6nh8zsz4fbpclm99r12jw39zrlzhhw",
      "type": "tarball",
      "url": "https://github.com/NixOS/nixpkgs/archive/79d3ca08920364759c63fd3eb562e99c0c17044a.tar.gz",
      "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
  }
}

Niv


{ overlays ? [], ... } @ args:
let
  sources = import nix/sources.nix;
  my-overlays = [ /* ... */ ];
in
import sources.nixpkgs(args // {
  overlays = overlays ++ my-overlays;
})
 ❤ (theia) ~/sys> niv update nixpkgs
Update nixpkgs-unstable
Done: Update nixpkgs-unstable

Flakes


Flakes allow you to specify your Nix dependencies in a declarative way.

# flake.nix
{
  inputs = {
    home-manager.url = "github:nix-community/home-manager";
  };
}
❤ (theia) ~> nix flake lock --update-input home-manager

Flake inputs


{
  description = "hello-flake";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
}

Flake outputs


{
  # ...

  outputs = { self, nixpkgs, ... }:
    let system = "x86_64-linux";
    in
      {
        defaultPackage."${system}" = (import nixpkgs { inherit system; }).hello;
      };

}

 ❤ (theia) ~> nix flake lock
...

 ❤ (theia) ~> ls
flake.lock  flake.nix

 ❤ (theia) ~> nix build

 ❤ (theia) ~> ls
flake.lock  flake.nix result

 ❤ (theia) ~> result/bin/hello
Hello, world!

{
  # ...

  outputs = { self, nixpkgs, home-manager, ... }:
    {
      nixosConfiguration."hyperion" = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          home-manager.nixosModules.home-manager
          ./roots/hyperion.nix
        ];
      };
    };
}

⚠️ Flakes are experimental