Overriding nixpkgs config in an overlay

I have figured out how to set nixpkgs.config within a flake, but that is not quite what I need to do. I need to set nixpkgs.config.cudaSupport = true on an overlay that I am using as an input to my own flake, because that overlay includes a package that is needs CUDA. (It can build without CUDA, but will throw an exception as soon as it tries to do anything.)

My project’s flake is as follows.

{
  #inputs.vs-overlay.url = "github:tadeokondrak/vs-overlay";
  inputs.vs-overlay.url = "git+file:///home/aidan/src/vs-overlay?ref=vsgan";
  inputs.vs-overlay.inputs.nixpkgs.follows = "nixpkgs";

  outputs = { self, nixpkgs, vs-overlay }: with nixpkgs;
    let
      system = "x86_64-linux";
    in {
      packages."${system}".default = legacyPackages.${system}.vapoursynth.withPlugins
        (with vs-overlay.packages.${system}.vapoursynthPlugins; [
          ffms2
          vsgan
        ]);
    };
}

I have a local fork of vs-overlay that has vsgan added. This package is the only one that needs CUDA, so I cannot simply add config.cudaSupport = true to vs-overlay, because then users could not use anything from this overlay without CUDA. Is what I need to do even possible with flakes at present? I know that there are still some limitations in flakes that prevents some developers from migrating their existing nix environments to flakes.

just evaluate nixpkgs yourself instead of using it’s legacyPackages output, i.e. let pkgs = import nixpkgs { config.cudaSupport = true; }; in #...

I tried that, but I get an error about currentSystem missing.

{
  #inputs.vs-overlay.url = "github:tadeokondrak/vs-overlay";
  inputs.vs-overlay.url = "git+file:///home/aidan/src/vs-overlay?ref=vsgan";
  inputs.vs-overlay.inputs.nixpkgs.follows = "nixpkgs";

  outputs = { self, nixpkgs, vs-overlay }: with nixpkgs;
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { config.cudaSupport = true; };
    in {
      packages."${system}".default = pkgs.${system}.vapoursynth.withPlugins
        (with vs-overlay.packages.${system}.vapoursynthPlugins; [
          ffms2
          vsgan
        ]);
    };
}
nix shell .#default
warning: Git tree '/home/aidan/src/vapoursynth-scripts' is dirty
warning: updating lock file '/home/aidan/src/vapoursynth-scripts/flake.lock':
• Updated input 'vs-overlay':
    'github:aidalgol/vs-overlay/22f16a802b9a60f60ac089170e54b4ea1c611287' (2022-05-20)
  → 'git+file:///home/aidan/src/vs-overlay?ref=vsgan&rev=e3634489e2537e8c06d81cc99b595bff171b56aa' (2022-05-20)
warning: Git tree '/home/aidan/src/vapoursynth-scripts' is dirty
error: attribute 'currentSystem' missing

       at /nix/store/s5vidnfp9kl1lqg3yriilizfiz07i57n-source/pkgs/top-level/impure.nix:17:43:

           16|   # (build, in GNU Autotools parlance) platform.
           17|   localSystem ? { system = args.system or builtins.currentSystem; }
             |                                           ^
           18|

forgot to mention to also inherit the system, that is import nixpkgs { inherit system; config.cudaSupport = true; }

Flakes don’t allow for impure operations like reading the current systems type. You have to explicitly define it instead

1 Like

That now evaluates, but does not seem to be applying to vs-overlay, since pytorch reports that CUDA is unavailable. (torch.cuda.is_available() returns False in a nix shell with this flake.)

1 Like

does this vs-overlay package actually provide an overlay? Seems like it should given the name. Your nixpkgs config doesn’t affect the packages you pull out of the flake because they are probably built with a different nixpkgs all together. Using the supplied overlay instead will add the packages to your nixpkgs instead. overlays is also an argument to be passed to nixpkgs during import, so assuming the flake does in fact export an overlay it would be import nixpkgs { inherit system; config.cudaSupport = true; overlays = vs-overlay.overlay; }. Then whatever packages it supplies can be pulled out of your nixpkgs instead

1 Like

This is what my flake looks like now, and the assert on torch.cuda.is_available() in my test script still fails.

{
  #inputs.vs-overlay.url = "github:tadeokondrak/vs-overlay";
  inputs.vs-overlay.url = "git+file:///home/aidan/src/vs-overlay?ref=vsgan";
  inputs.vs-overlay.inputs.nixpkgs.follows = "nixpkgs";

  outputs = { self, nixpkgs, vs-overlay }: with nixpkgs;
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        config.cudaSupport = true;
        overlays = [ vs-overlay.overlay ];
      };
    in {
      packages."${system}".default = pkgs.vapoursynth.withPlugins
        (with vs-overlay.packages.${system}.vapoursynthPlugins; [
          ffms2
          vsgan
        ]);
    };
}

I am fairly certain vs-overlay is really an overlay. This is its flake file:

{
  description = "vs-overlay";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  outputs = { self, nixpkgs }:
    let
      systems = [ "x86_64-linux" "aarch64-linux" "i686-linux" "x86_64-darwin" ];
      forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
      nixpkgsFor = forAllSystems (system:
        import nixpkgs {
          inherit system;
          overlays = [ self.overlay ];
          config.allowUnfree = true;
        }
      );
    in
    {
      overlay = import ./default.nix;
      packages = forAllSystems (system: {
        inherit (nixpkgsFor.${system})
          getnative
          vapoursynthPlugins;
      });
    };
}

While the flake has overlay in the name and does indeed contain an overlay, it is not the only thing it provides. In addition to an overlay (a mechanism to extend Nixpkgs package set consistently), the flake also offers a pre-composed package set in the packages output, which applies the overlay to its own copy of Nixpkgs. And since you rely on the latter to get plug-ins, it will not see any options you instantiated your Nixpkgs with.

Now that you have added the overlay to your pkgs, you should be just able to use pkgs.vapoursynthPlugins (assuming the overlay is defined correctly in the flake).

1 Like

That would explain why I found this overlay particularly confusing. Thanks to everyone for their help!