what is it?

a system for referencing and sharing nix code

a nix flake is a directory with a flake.nix and flake.lock at the root that outputs nix expressions that others can use to do things like build packages, run programs, use development environments, or stand up NixOS systems. If necessary, flakes can use the outputs of other flakes as inputs

it is currently an experimental feature, but chances are good that it will become official. flake’s biggest claim to fame is that it version-pins its dependencies in a lock file, improving reproducibility

installation

NixOS

edit configuration.nix (usually in /etc/nixos/) and add this line

configuration.nix
nix.settings.experimental-features = [ "nix-command" "flakes" ];

run sudo nixos-rebuild switch

create a flake.nix file in the same directory as your configuration.nix

flake.nix
{
  description = "Nixos config flake";
 
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
 
    # home-manager = {
    #   url = "github:nix-community/home-manager";
    #   inputs.nixpkgs.follows = "nixpkgs";
    # };
  };
 
  outputs = { self, nixpkgs, ... }@inputs: {
    nixosConfigurations.<hostname> = nixpkgs.lib.nixosSystem {
      specialArgs = {inherit inputs;};
      modules = [
        ./configuration.nix
        # inputs.home-manager.nixosModules.default
      ];
    };
  };
}

change <hostname> to the value of networking.hostName in the configuration.nix file. that way only sudo nixos-rebuild switch is needed to rebuild the configuration. (if <hostname> was something arbitrary like default, the command would be sudo nixos-rebuild switch --flake /etc/nixos#default )

update

nix flake update