How to add a flake package to system configuration

I have made a neovim flake, which includes plugins and custom rc, this is made from unstable, because I need 0.5 version neovim

# neovim flake.nix
{
  description = "neovim wrap";

  inputs = {
      nixpkgs-unstable.url = github:NixOS/nixpkgs/nixos-unstable;
      flake-utils.url = github:numtide/flake-utils;
  };

  outputs = { self, ... }@inputs:
    inputs.flake-utils.lib.eachDefaultSystem (system:
        let
          pkgs = import inputs.nixpkgs-unstable {
            overlays = [
              (import ./nvim_overlay.nix) 
            ];
            inherit system;
          };
        in {
          defaultPackage = pkgs.sheeaza.nvim;
        }
    ) // {
      overlay = (import ./nvim_overlay.nix);
    };
}

so the question is how to add this to nixos system flake configurations? I will use stable version 21.05 for my system, and customed unstable neovim.

#system flake.nix
{
  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixos-21.05;
    nixpkgs-unstable.url = github:NixOS/nixpkgs/nixos-unstable;
    flake-utils.url = github:numtide/flake-utils;
    nvim.url = "/home/xxx/project/nvim"; # points to the unstable neovim above
  };

  outputs = { self, ... }@inputs: {
    nixosConfigurations.xxx = inputs.nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [ 
        ./configuration.nix
      ];
      specialArgs = { inherit inputs; };
    };
  };
}
1 Like

Local flakes can be used with

nvim.url = "path:/home/xxx/project/nvim";
1 Like

Thanks, and after nvim.url = "path:/home/xxx/project/nvim"; added to inputs, how should I add the nvim package to the configuration.nix

1 Like

Pass that (and potentially other inputs) to your _module.args and the you can “pull” the nvim flake from the module arguments and install it.

How I prepare the _module.args can be seen on line 12 of the linked function, though I have to admit, for some reason I do it a bit complicated :smiley:

And then I just add the inputs as necessary to the modules argset, as can be seen in my “flake enabler”, which by default uses nix from master:

1 Like

You are forwarding all inputs as arguments to configuration.nix with the line

specialArgs = { inherit inputs; };

(which by the way is equivalent to specialArgs = inputs;).

Hence in your configuration.nix you can do

{ config, pkgs, inputs, ... }: {
  # ...
  environment.systemPackages = [ inputs.nvim.defaultPackage.x86_64-linux ];
  # ...
}

or if you prefer using your overlay

{ config, pkgs, inputs, ... }: {
  # ...
  nixpkgs.overlays = [ inputs.nvim.overlay ];
  environment.systemPackages = [ pkgs.neovim ];
  # ...
}
2 Likes

No, it is not, it would be equivalent to specialArgs.inputs = inputs.

Also specialArgs doesn’t compose very well. _module.args should be preferred.

1 Like

Thanks for this example, but I am still new to nix and this is too complicated for me, I cannot hold this. :rofl:

How exactly is it too complicated?

It is setting some option through the modules argument you pass to your nixosSystem call, then reusing that from your actual module file.

It is just these 2 steps. I could PR that for you if you tell us the location of your repository.

1 Like

Thanks for your help,
background:
I have learned basicly nix lauguage, like functions, attribute, but I am still puzzled with the predefined nix function. The purpose for me to use nix is simple, to make my development environment totally reproducible, I have gotten tired of repeating configuring system, like editor, cmdline tool stuff

I have uploaded my neovim flake here: https://github.com/sheeaza/nix-nvim, this could basicly work
and system flake here: GitHub - sheeaza/nix-system, this is still in progress and does not work now

I did a PR that installs your nvim from the defaultPackage of the nvim flake you provided.

https://github.com/sheeaza/nix-system/pull/1

It built for me without any issues, of course I can not really test it.

PS: In general, I am not a fan of overlays, especially not in combination of flakes. If something can be done without them, I do it without them.

2 Likes

Oh! Oh? Can you expand on this? Thanks!

Also, isn’t it possible that the actual package consumption is less-trivial? I find myself often having to do:

environment.systemPackages = with pkgs; [
  inputs.nvim.packages.x86_64-linux.neovim
];

I do not really understand it, though thats what I read from this comment, which one eventually ends up reading if one follows the implementation of nixosSystem:

Also, I read somewhere, that extraArgs and specialArgs do not compose well and forbid the use of _module.args, while one can use _module.args in many modules and it will properly merge.

If it is just for one or two packages, I tend to do it like this and write the attributes out fully. If it is more than that I use let or with with a narrow scope.

Thanks again for your help, it works now, I will continue on bringing up tmux fish config.

I would just like to mention that a very similar situation lead me to start using DevOS as a system for managing A flakes-based NixOS config.

I find the vanilla experience extremely un-ergonomic and DevOs provides a nice wrapper that allows you to access things in a much more intuitive way.

Under that system there are simple ways to add packages or overlays to the nixpkgs you’re passing around to modules.

1 Like