Skip to content

Clean up the Nix flake, recommended practices, provide overlay#6

Merged
daylinmorgan merged 14 commits intodaylinmorgan:mainfrom
spikespaz-contrib:main
Feb 10, 2024
Merged

Clean up the Nix flake, recommended practices, provide overlay#6
daylinmorgan merged 14 commits intodaylinmorgan:mainfrom
spikespaz-contrib:main

Conversation

@spikespaz
Copy link
Contributor

This is how I organize my Nix flakes and allow for the utmost compatibility for any desired use-case. When I say "recommended practices", they are my own, but also very similar to the "best practices" purported by the community.

I recommend reviewing each commit individually.

@daylinmorgan
Copy link
Owner

I recommend reviewing each commit individually.

Some of these changes aren't really independent, but I'll try.

Here are some high-level thoughts:

These changes broke nix flake check and nix flake show

❯ nix flake check
error:
       … while checking flake output 'overlays'

         at /nix/store/pps7hwqysf2l449krqg70yx3z2gslhbj-source/flake.nix:20:7:

           19|     in {
           20|       overlays = {
             |       ^
           21|         default = pkgs: pkgs0: {

       … while checking the overlay 'overlays.default'

         at /nix/store/pps7hwqysf2l449krqg70yx3z2gslhbj-source/flake.nix:21:9:

           20|       overlays = {
           21|         default = pkgs: pkgs0: {
             |         ^
           22|           monolisa-nerdfont-patch = pkgs.stdenv.mkDerivation {

       error: overlay does not take an argument named 'final'
❯ nix flake show
git+file:///home/daylin/stuff/fonts/monolisa-nerdfont-patch?ref=refs/heads/spikespaz/main&rev=fa54af9cba60c7ff2e2928a5dbe95800f4bc57dd
├───devShells
│   ├───aarch64-linux
│   │   └───default omitted (use '--all-systems' to show)
│   └───x86_64-linux
│       └───default: development environment 'nix-shell'
├───formatter
│   ├───aarch64-linux omitted (use '--all-systems' to show)
error:
       … while evaluating the attribute 'packages.x86_64-linux.default'

         at /nix/store/96dn6crb2c2l9mwv64a0yq7rnwbf9v6h-source/flake.nix:73:11:

           72|         packages = rec {
           73|           default = nixfmt;
             |           ^
           74|           nixfmt = pkgs.haskellPackages.nixfmt;while evaluating the attribute 'haskellPackages.nixfmt'

         at /nix/store/96dn6crb2c2l9mwv64a0yq7rnwbf9v6h-source/flake.nix:31:15:

           30|             packageOverrides = self: super: {
           31|               nixfmt = self.callCabal2nix "nixfmt" src { };
             |               ^
           32|             };

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: cannot build '/nix/store/qqkh6wvkk8y3ansvbcxxq2ysgdnh07dy-cabal2nix-nixfmt.drv^out' during evaluation because the option 'allow-import-from-derivation' is disabled

With regards to adding nixfmt. Seems unnecessary to me to add a formatter for a single (< 100LOC) .nix file that probably won't even change much after this (until nix breaks something). But, also as a flake input it adds OVER 60 additional inputs for little benefit! (FWIW I usually use alejandra).

Using nix-systems is fine with me, but the community consensus seems quite unsettled on that being the appropriate solution to a perceived problem with defining outputs.

Copy link
Owner

@daylinmorgan daylinmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to keep a forAllSystems function that that takes pkgs something like:

forAllSystems = f:
 genAttrs (import systems)
  (system:
     f (import nixpkgs {
       localSystem.system = system;
       overlays = [ self.overlays.default ];
     }));

Removing the formatter output removes the standalone use for the eachSystem function and then negates the redundant let pkgs = pkgsFor.${system} everywhere else.

@spikespaz
Copy link
Contributor Author

spikespaz commented Feb 10, 2024

The problem with Alejandra is that it causes very noisy diffs, which is a pain I contend with every time I rebase a PR. Bear with me.

@spikespaz
Copy link
Contributor Author

Take this one for example. It messes with indentation so much. I really don't like Alejandra.

@spikespaz
Copy link
Contributor Author

And this one, the diff was much nicer before.

@spikespaz
Copy link
Contributor Author

Some of these changes aren't really independent, but I'll try.

The commits are as they are because I have experience with people telling me no to Nixfmt, and this makes the rebase easier when I inevitably replace it with Alejandra. :)

@spikespaz
Copy link
Contributor Author

If you don't already know, Alejandra makes manual merges painful.

@spikespaz
Copy link
Contributor Author

That should be all the changes you asked for done.

@daylinmorgan daylinmorgan merged commit 80c347e into daylinmorgan:main Feb 10, 2024
@daylinmorgan
Copy link
Owner

Thanks!

src = ./.;
nativeBuildInputs = with final; [makeWrapper];
buildInputs = with final; [fontforge python3];
nativeBuildInputs = with pkgs; [makeWrapper];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed, this change is bad, don't ever use an external pkgs from inside an overlay. You should treat final as if it were pkgs, otherwise you evaluate Nixpkgs stages twice. See here for some relevant discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind the above comment. I now see line 26. If this is your preference (and you should be consistent with your change on lines 16-22) consider using the naming scheme that I have come up with in the thread I have already linked. It does make sense to rename final to pkgs, but as final/self and prev/super can also be used for overrideAttrs (and others) inside overlays, to avoid shadowing, consider pkgs: pkgs0.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using the naming scheme that I have come up with in the thread I have already linked. It does make sense to rename final to pkgs, but as final/self and prev/super can also be used for overrideAttrs (and others) inside overlays, to avoid shadowing, consider pkgs: pkgs0.

For one they aren't used within this overlay so avoiding name confusion is a moot point. And secondly, your scheme is incompatible with nix flake check as I previously demonstrated when you first submitted a PR with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, nixfmt caused the check failure. Not this.

postFixup = ''
wrapProgram $out/bin/monolisa-nerdfont-patch \
--set PATH ${lib.makeBinPath (with final; [fontforge])}
--set PATH ${makeBinPath (with final; [fontforge])}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is subjective, and I understand that it is preference, but parts of Nixpkgs are moving away from this, which is why I changed it. The idea is to keep it clear what is in lib and what is in the prelude. You may safely ignore this comment, especially since I can't find the relevant discussion that I read and I can provide no evidence other than anecdotal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the issue I am thinking of in Nixpkgs is the over-usage of with which causes name-clashing. I don't remember well.

devShells = eachSystem (_: pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [fontforge python3 pre-commit];
buildInputs = with pkgs; [fontforge python3 pre-commit];
Copy link
Contributor Author

@spikespaz spikespaz Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, not a suggestion or correction: why did you choose to use buildInputs over packages?

Now a little pushy: mkShell is a smart function, is documented, and it does not suggest using buildInputs (though this is common practice). See the manual.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make no difference they are all just merged.

});

formatter = eachSystem (system: _: alejandra.packages.${system}.default);
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand not wanting to take the formatter's flake as an input, however consider re-introducing the formatter output because it is special. You can use the package from Nixpkgs instead. The output allows nix fmt to work. This is useful: I manually run nix fmt to keep myself organized while I am working on dirty code, and additionally, configure my editor to format the code using the nix fmt command when the file is saved. This allows me to write long lines, and then semi-automatically format to keep my head clear and the code readable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am well aware of how nix fmt works.

@spikespaz
Copy link
Contributor Author

I have added comments, as I am sure your inbox is well-aware. I probably spammed you while struggling with the GitHub UI. It seemed to post them in the wrong locations, and when I went to correct them, refreshing the page showed that I had double-posted, edited, or even had it correct in the first place. I have absolutely no idea what was happening; I do sincerely apologize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants