Adding a flake to environment.systemPackages

I have a GitHub repo that builds using a flake. (I.e., it has a a flake.nix, and a flake.lock). I’ve built and tested it, and now I’d like to add it to my NixOS set-up.

I’m trying to adapt this example from the NixOS manual

In my NixOS configuration, I have:

  environment.systemPackages = [
    . . .
    (import ./pandoc-select-code.nix)
    . . .

And here’s pandoc-select-code.nix:

with import <nixpkgs> {}; # bring all of Nixpkgs into scope

stdenv.mkDerivation rec {
  name = "pandoc-select-code";
  src = builtins.fetchGit {
    url = "";
    ref = "HEAD";

But when I do sudo nixos-rebuild switch --upgrade, I get the following errors. Does mkDerivation support flakes?

unpacking channels...
building Nix...
building the system configuration...
these 12 derivations will be built:
building '/nix/store/4gd4js7w1sdlzf8walgginn3akambdpg-pandoc-select-code.drv'...
unpacking sources
unpacking source archive /nix/store/yl8n55bxr85y0a37g0hi32w4p8ml347r-source
source root is source
patching sources
no configure script, doing nothing
no Makefile, doing nothing
install flags: SHELL=/nix/store/4nmqxajzaf60yjribkgvj5j54x9yvr1r-bash-5.1-p12/bin/bash install
make: *** No rule to make target 'install'.  Stop.
error: builder for '/nix/store/4gd4js7w1sdlzf8walgginn3akambdpg-pandoc-select-code.drv' failed with exit code 2
error: 1 dependencies of derivation '/nix/store/sllghpxvry7dbzfjf3ai4fnsz23c55n8-system-path.drv' failed to build
error: 1 dependencies of derivation '/nix/store/ybfi9kl83wnr8kwqlkgjknnk8ywxwayw-nixos-system-wombat11k-22.05pre356435.7f9b6e2babf.drv' failed to build

The way I do this in my nixos-configs is by:

  1. Adding the other flake as an input;
  2. Adding that flake’s package to my system flake’s packages;
  3. Adding my system flake’s packages as a flakePkgs attribute via specialArgs; and
  4. Referencing the package I want in my config as flakePkgs.<package>.
1 Like

I haven’t yet switched my NixOS config to flakes, but that sounds like the way to go when I do.

I don’t use flakes, but I do something similar on one of my machines. As far as I can tell, you currently have two problems, but you’re almost there.

The example from the NixOS manual is essentially showing how to both package a GNU autotools based program (the my-hello.nix file) and add it to your configuration (the snippet about systemPackages). Using stdenv.mkDerivation isn’t working because it expects a makefile etc., but you already have your program packaged in your flake so I don’t think you need it.

The glue to connect your flake into your non-flake config is flake-compat. You add it to the inputs of your flake, and use the example in the readme to create a default.nix in your project root. Then, your flake can be built by a non-flake nix.

In your NixOS configuration you can just import the entire flake inside your environment.systemPackages, and NixOS should understand that it can build the default.nix and add the resulting package to your system.

1 Like

Just to be clear, that means after adding flake-compat you change pandoc-select-code.nix to:

import (builtins.fetchGit {
  url = "";
  ref = "HEAD";

or your NixOS configuration to something like

  pandoc-select-code = import (builtins.fetchGit {
    url = "";
    ref = "HEAD";
in environment.systemPackages = [
  . . .
  . . .

Those should be equivalent ignoring any typos, so it’s just a style choice. I’ve seen both online though.

1 Like

If you don’t want to convert your system configuration to be defined in a flake but nevertheless you are using Nix 2.4+ you can use the still undocumented builtin builtins.getFlake aFlakeURL that allows you to load a flake from Nix code. You can try it out in the repl.

1 Like

For the benefit of others, here’s the solution that I went with. I’m not using the pandoc-select-code.nix file now; all I need is one line in my NixOS configuration.

  environment.systemPackages = [
    . . .
    (builtins.getFlake "github:mhwombat/pandoc-select-code")
    . . .

Thank you so much @reckenrode , @kamron_m , and @azazel75 for the explanations.

1 Like