this post was submitted on 31 Aug 2023
4 points (100.0% liked)

nixos

1233 readers
1 users here now

All about NixOS - https://nixos.org/

founded 4 years ago
 

I need to generate a number of scripts in my configuration and make them into a single package (for ease of reference, because there are a lot of them).

So far, I'm creating the scripts via writeShellApplication, making them into packages via an overlay, merging them with buildEnv and then adding the resulting package to `systemPackages.

Something like:

nixpkgs.overlays = [ (final: prev: {
  my-hello-1 = final.writeShellApplication {
    name = "my-hello-1-script";
    text = "echo my hello wolrd 1";
  };
  my-hello-2 = final.writeShellApplication {
    name = "my-hello-2-script";
    text = "echo my hello wolrd 1";
  };
  my-hello-scripts = final.buildEnv {
    name = "my-hello-scripts";
    paths = [ final.my-hello-1 final.my-hello-2 ];
  };
}) ];

environment.systemPackages = [ pkgs.my-hello-scripts ];

This works, but I don't really need the my-hello-1 and my-hello-2 packages... can you think of a way to make do without needing them?

top 5 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 11 months ago (1 children)

Why the overlay? If you just want to give the drvs names (good practice IMO), simply use a let binding.

The buildEnv is unnecessary: systemPackages does the same with all the derivations in the list in the end anyways.

[–] [email protected] 2 points 11 months ago* (last edited 11 months ago)

If you just want to give the drvs names [...], simply use a let binding.

I must be missing something here.. my first idea was to put all the writeShellApplications inside systemPackages (with no let bindings: the scripts are generated from config anyway), but it resulted in nixos complaining that it was expecting actual packages.

Edit: scratch that - I was being stupid :)

[–] Klaymore 2 points 11 months ago* (last edited 11 months ago)

There's probably a cleaner way to do this, but you can look at abstractions as a way to reduce all that code repetiton.

[–] [email protected] 1 points 11 months ago (1 children)

Don't worry about the extra derivations. Nix is full of them.

[–] [email protected] 1 points 11 months ago* (last edited 11 months ago)

Well, it does work as-is, and it's not like I'm worried how many symlinks need to be dereerenced... the point is mainly that my nix code could be much simpler if I didn't have to build the overlay attrset like that from a list.