Minimum viable example of a flake in home.nix?

I’m still trying to grok flakes (and to an extent Nix/OS as a whole). I’ve been looking at other people’s configs to try and see what’s what, but it’s still very difficult for me to decipher exactly just what is what.

Could someone provide a minimal example of using a flake to declaratively install a program in home.nix? For example, the Neovim flake at github:neovim/neovim?dir=contrib?

Ideally in the form of

# In ~/.config/nixpkgs/home.nix
{ config, pkgs, ... }:

{
  programs.home-manager.enable = true;
  home.username = "myUsername";
  home.homeDirectory = "/home/myUsername";
  home.stateVersion = "21.03";

  #
  # 
  # Code that installs Neovim here using its flake at
  # github:neovim/neovim?dir=contrib
  #
  #  
}

where I could then run home-manager switch and be able to access Neovim via running nvim.

1 Like

home.nix is not flake aware without some extra lifting.

The easiest way is to load the flake using flake-compat.

1 Like

I do not understand where this integrates into my solution/example, or what to do afterwards in order to utilize it.

Your example doesn’t even show what you want to do with the flake, therefore its hard to say.

But roughly:

{ pkgs, ... }:

let
  fc = import (fetchFromGitHub {
    owner = "edolstra";
    repo = "flake-compat";
    ref = …;
    sha256 = …;
  });
in {
  nixpkgs.overlays = [ (fc { src = "github:neovim/neovim?dir=contrib"; }).defaultNix.overlay) ];
}

At least if you want to use the overlay.

Problem is: They do not provide a flake.lock this might break the flake-compat function, as well as it will create warnings when used within a regular flake.

They should provide a lock file to make the flake complete and usable.

1 Like

I’m not a flake user so take what I say with a grain of salt, but the flake in the neovim repo seems to be meant for developing neovim, not installing the package. I imagine that with nix installed and flakes enabled, a developer would make changes to the neovim source code in a local checkout of the repo, then cd into contrib and test the changes out by running a command like nix build . to build neovim or nix run .#nvim-debug to build and run neovim with debug settings.

If you’re new to Nix/NixOS I wouldn’t necessarily recommend learning flakes yet. They’re still experimental and as a user of NixOS and home-manager for about two years now, they haven’t felt well documented or easy enough to switch my configurations over to using them, though nix-flk has been very tempting. I would recommend doing a pretty vanilla home-manager installation in whatever linux distro you already have if you’re just looking for a declarative user environment, installing NixOS if you want more declarative system services, and using flakes if you need the reproducibility it provides.

1 Like

I know I’m still not really answering you’re question, but since I don’t use flakes this would be the way I would install neovim with home-manager:

# ~/.config/nixpkgs/home.nix
{ pkgs, ... }

{
  programs.home-manager.enable = true;
  programs.neovim.enable = true;
}

I typed this directly into discourse from mobile so I may have mistakes. Hopefully someone with experience using flakes could give you a more satisfying answer.