How to use unmodified default.nix from nixpkgs

I’m modifying an existing program and I want to use a copy of the default.nix from nixpkgs to build my software. I don’t know how to expand pkgs or stdenv for use with callPackage and get the error:

error: evaluation aborted with the following error message: 'lib.customisation.callPackageWith: Function called without required argument "mkDerivation" at /nix/store/8yl19vnr44klhwjs0yr1ksjzj992z8yc-source/pkgs/my_openscad/default.nix:29'

I could manually pass in mkDerivation or modify default.nix to use pkgs.mkDerivation, but that can get tedious on large projects and makes upstream submissions harder.

Is there a way I can use callPackage that will work with the upstream nix file unmodified?

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in {
      packages.${system} = {
        openscad = pkgs.callPackage ./default.nix {};
      };

      defaultPackage.${system} = self.packages.${system}.openscad;
    };
}

Package I’m building: nixpkgs/pkgs/applications/graphics/openscad/default.nix at 9258185510b8cf0a67251eaf9dc9bc6bf87aa68c · NixOS/nixpkgs · GitHub

If you want to use it unmodified you’d use pkgs.openscad. If you want to modify it you’d use pkgs.openscad.override (or overrideAttrs as per the usecase). I find it unclear what you want to achieve though.

I want to change src in default.nix to point to a modified repository but I don’t want to make other changes to default.nix. This is a general issue I have with modifying nixpkgs.

I want to be able to call default.nix the way nixpkgs does, but from inside a flake.

That sounds like a bog-standard overrideAttrs use case. You aren’t ‘calling default.nix’, you’re taking an existing derivation and changing one of its attributes. You just need to do this:

pkgs.openscad.overrideAttrs {
  src = <fetcher for your modified repository here>;
}
2 Likes

That’s helpful for this specific case, but I still have the problem of testing packages when writing new packages for nixpkgs. I want to pass in whatever nixpkgs does when it calls these packages.

I absolutely cannot figure out what you are trying to accomplish. If you want to modify nixpkgs, just clone it / modify it, build it? I have a copy of nixpkgs with a modified copy of the metals pkg on it, if I want to build it I just do?

~/src/thirdparty/nixpkgs metals-final-attrs                                                  ✘ 128
❯ nix repl --file './.'
Nix 2.24.14
Type :? for help.
Loading installable ''...
Added 24387 variables.

nix-repl> :b metals     

This derivation produced the following outputs:
  out -> /nix/store/lcn2bqsdh40ly4niym2jdigk4jwc5xsp-metals-1.5.2
[159 copied (1074.6 MiB), 585.5 MiB DL]
nix-repl> 

I guess if you want to understand how to make something stand-alone then does this help? Here is some rando non-nixpkgs nix file that I found kicking around my src directory.

{
  callPackage,
  fetchFromGitHub,
}:
let
  pnpm2nixRepo = fetchFromGitHub rec {
    owner = "nzbr";
    repo = "pnpm2nix-${owner}";
    rev = "0366b7344171accc2522525710e52a8abbf03579"; # current HEAD of master
    hash = "sha256-ytyTwNPiUR8aq74QlxFI+Wv3MyvXz5POO1xZxQIoi0c=";
  };
  pnpm2nix = callPackage (import "${pnpm2nixRepo}/derivation.nix") { };
in
pnpm2nix.mkPnpmPackage {
  src = fetchFromGitHub {
    owner = "lukin";
    repo = "keywind";
    rev = "master";
    hash = "sha256-8N+OQ6Yg9RKxqGd8kgsbvrYuVgol49bo/iJeIJXr3Sg=";
  };
  script = "build:jar";
  distDir = "out";
}

If I wanted to build it (assuming I have a nixpkgs on my nix search path) this can be built with nix-build by calling it with callPackage

nix-build -E '(import <nixpkgs> {}).callPackage ./ci/keywind.nix {}'

does that help?

I figured it out: there are different variants of callPackage being used in all-packages.nix.

  openscad = libsForQt5.callPackage ../applications/graphics/openscad { };

In this example if I use pkgs.libsForQt5.callPackage instead of pkgs.callPackage it works as expected.

I know there are many ways to build and override packages, I just wanted to know what I was doing differently compared to nixpkgs.

2 Likes