I create a nix function that runs a shell script. I’m using flake, and it works fine if I define the function directly in my flake.nix. I want to extract the function into a separate nix file because I don’t like defining everything in flake.nix. But I cannot figure out how to do this seemingly simple task.
error: attempt to call something which is not a function but a set
at /nix/store/9nan3naknbb1fv1y64rx6v33449zmzby-source/flake.nix:43:20:
42|
43| switch = createScript {
| ^
44| name = "switch";
Or should I try to achieve this via nix flake? I’m not sure what’s the recommend approach.
Both lines are the same. I don’t have the other files such as ./scripts. I created a temp script to test what you are doing
# temporary script name that echos "sup" and does not use `readFile`.
# I did this since I don't have the contents of `./scripts`
((writeScriptBin name ''echo sup'').overrideAttrs (old: {
buildCommand = "${old.buildCommand}\n patchShebangs $out";
}))
❯ nix repl .
Welcome to Nix 2.18.1. Type :? for help.
Loading installable 'git+file:///Users/sebas/repos/help#'...
Added 1 variables.
nix-repl> :lf . # Load the nix flake
Added 12 variables.
nix-repl> :b packages.aarch64-darwin.install # build the package "install" in aarch64-darwin
This derivation produced the following outputs:
out -> /nix/store/589dplvhsnjw0i6i62xb697p6jr9f8s1-install
# you no longer need to call the attribute of `createScript` from `create.nix`
# since it does not exist any more with this new example.
createScript = pkgs.callPackage ./create.nix {};
Thanks, I also figured out that create.nix was passing an attribute set since it had a property called createScript.
As you suggested, using createScript = (pkgs.callPackage ./create.nix {};).createScript; should work, but since the only thing this file outputs is the function, I modified it just return the function only.
I also had to add name and dependencies to the function otherwise it did not recognize the arguments.