Adding Custom Package on NixOS Flake

When I build via nixos-rebuild switch, the following error shows:

error: undefined variable 'vi-vim-symlink'

       at /nix/store/wjcq0y0r5l4fvh562d9qxh58x6vc646z-source/hosts/s210-touch/default.nix:142:5:

          141|     neovim-unwrapped
          142|     vi-vim-symlink
             |     ^
          143|   ];

flake.nix output part:

  outputs = { self, nixpkgs, ... }@inputs:
    let
      inherit (self) outputs;
      forEachSystem = nixpkgs.lib.genAttrs [ "x86_64-linux" ];
      forEachPkgs = f: forEachSystem (sys: f nixpkgs.legacyPackages.${sys});
 
      mkNixos = modules: nixpkgs.lib.nixosSystem {
        inherit modules;
        specialArgs = { inherit inputs outputs; };
      };
    in
    {
      overlays = import ./overlays { inherit inputs outputs; };
      packages = forEachPkgs (pkgs: import ./pkgs { inherit pkgs; });
 
      nixosConfigurations = {
        s210-touch = mkNixos [ ./hosts/s210-touch ];
      };
  };

pkgs/default.nix

{ pkgs ? import <nixpkgs> { } }: {
  vi-vim-symlink = pkgs.callPackage ./vi-vim-symlink { };
}

hosts/s210-touch/default.nix systemPakcages part

  environment.systemPackages = with pkgs; [
    kwayland-integration
    sddm-kcm
    plasma-systemmonitor
    systemsettings
    kde-gtk-config
    konsole
    kate
    firefox
    keepassxc
    mpv
    git
    syncthing
    postman
    gparted
    chezmoi
    neovim-unwrapped
    vi-vim-symlink
  ];

I am not sure what the real content of pkgs/default is (as you pasted the same content as for flake.nix), but you probably want self.packages.${pkgs.system}.vi-vim-symlink.

To be able to use that, you also need to pass self to your modules. The blog post linked below gives a brief explanation of various ways passing inputs around.

1 Like

Sorry I didn’t notice that I copied the wrong content for pkgs/default.nix. I updated the main post.

This is a really great blog post. Thank you, @NobbZ!