Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Nix Flake & make it easier to customize #12831

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

RossSmyth
Copy link
Contributor

@RossSmyth RossSmyth commented Feb 10, 2025

Hello. It has been a while since #11528. I am better at Nix, so I decided to take a crack at cleaning the flake up.

My main issues with it is that it was not setup to be very customizable. Also the Cachix cache is nearly useless since it is compiled with target-cpu=native, so the cache will nearly always be missed. So I refactored it and added some comments.

  1. I am unconvinced it needs all the source filtering.
  2. By default everything uses the MSRV compiler as that is a safe default.
  3. Cleaned up a few vestigial things that my guess is were left from earlier revisions

I did preserve the behavior that the devshell will compile with target-cpu=native and other flags. But now the Cachix cache is actually possible to hit.

You can see an example of it in use in my NixOS config here: https://github.com/RossSmyth/nixos/blob/338fce6a5b7c3c5fe9c1790929bae40ce1494006/home-manager/helix.nix#L10-L29

The main issue is that the previous weird setup may be relied one by some people. So this will break them.

Commits can be squashed when merged.

@the-mikedavis
Copy link
Member

I was considering trying to remove the dependency on crane and only use rust-overlay directly (for example like nil does) but I'm not sure if that's an easy change. I haven't taken advantage of the checks part of the flake so I think it's fine to drop that part.

Removing target-cpu=native from the package build but keeping it in the shell sounds good.

Should we also consider switching to the opt profile for flake builds? (See #12453 for context - opt is what we use for releases)

@RossSmyth
Copy link
Contributor Author

RossSmyth commented Feb 10, 2025

I was considering trying to remove the dependency on crane and only use rust-overlay directly

Yeah I considered that but also didn't think it was a big deal. I don't think it would be very hard to move to that. I can take a look later to see. It should be even easier to customize with only rust-overlay.

I haven't taken advantage of the checks part of the flake so I think it's fine to drop that part.

It's not a maintenance burden so there's not really a reason to remove it imo.

Should we also consider switching to the opt profile for flake builds?

Ah, I did not look at this. Sure, makes sense. FYI fat LTO is sort of a placebo effect now days since it doesn't get much attention in LLVM anymore. The main thing it does is make your builds take longer. Thin is generally pretty close or faster (at runtime) in many cases since Google and other folks put money into thin LTO (for chrome and such).

@RossSmyth
Copy link
Contributor Author

RossSmyth commented Feb 11, 2025

@RossSmyth
Copy link
Contributor Author

Moving to the Rust-Overlay only version would still be a net win though because Cachix would work. And it wouldn't be much different from the status-quo wrt customization.

@RossSmyth
Copy link
Contributor Author

RossSmyth commented Feb 12, 2025

@the-mikedavis the-mikedavis added the A-packaging Area: Packaging and bundling label Feb 17, 2025
@the-mikedavis
Copy link
Member

the-mikedavis commented Feb 26, 2025

On this branch I'm seeing an error when creating the dev shell:

$ nix --version
nix (Nix) 2.24.11
$ nix develop
evaluation warning: rustPlatform.rust.rustc is deprecated. Use rustc instead.
evaluation warning: rustPlatform.rust.cargo is deprecated. Use cargo instead.
error:
       … while calling the 'derivationStrict' builtin
         at <nix/derivation-internal.nix>:34:12:
           33|
           34|   strict = derivationStrict drvAttrs;
             |            ^
           35|

       … while evaluating derivation 'nix-shell'
         whose name attribute is located at /nix/store/sdzpqjwx7pdx6lsq6llyfqqf7hspp83c-source/pkgs/stdenv/generic/make-derivation.nix:336:7

       … while evaluating attribute 'buildInputs' of derivation 'nix-shell'
         at /nix/store/sdzpqjwx7pdx6lsq6llyfqqf7hspp83c-source/pkgs/stdenv/generic/make-derivation.nix:383:7:
          382|       depsHostHost                = elemAt (elemAt dependencies 1) 0;
          383|       buildInputs                 = elemAt (elemAt dependencies 1) 1;
             |       ^
          384|       depsTargetTarget            = elemAt (elemAt dependencies 2) 0;

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

       error: cannot coerce a set to a string: { bindgenHook = «thunk»; buildRustPackage = «thunk»; cargoBuildHook = «thunk»; cargoCheckHook = «thunk»; cargoInstallHook = «thunk»; cargoNextestHook = «thunk»; cargoSetupHook = «thunk»; fetchCargoTarball = «thunk»; importCargoLock = «thunk»; maturinBuildHook = «thunk»; «3 attributes elided» }

The package build with nix build works however

@RossSmyth
Copy link
Contributor Author

Oh yeah, I did not test that.

flake.nix Outdated
Comment on lines 69 to 70
rustPlatform.rust.rustc # TODO: Remove
rustPlatform.rust.cargo # TODO: Remove
Copy link
Member

Choose a reason for hiding this comment

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

Should we use rustc and cargo from the attrs passed to mkHelix instead? I'm seeing a deprecation warning on accessing these

Copy link
Contributor Author

@RossSmyth RossSmyth Feb 27, 2025

Choose a reason for hiding this comment

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

No as they are not spliced in properly if you do that. The alternative is to change the callPackage invocations to be something like

pkgs.callPackage mkHelix {
    rustPlatform = msrvPlatform;
    rustc = msrvToolchain.rustc; 
    cargo = msrvToolchain.cargo;
};

Copy link
Member

Choose a reason for hiding this comment

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

For the package build (not the shell) we don't need to follow the MSRV, it's ok to build with latest stable. We do roughly the same in CI - the build workflow for PRs checks with the MSRV and then the release workflow uses whatever is the latest stable

Copy link
Contributor Author

@RossSmyth RossSmyth Feb 27, 2025

Choose a reason for hiding this comment

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

This is not for Helix, but rather for me (or anyone else who overrides the toolchain) so that when I override the toolchain that the correct Cargo and Rustc are used for the entire build.

@RossSmyth
Copy link
Contributor Author

Hmm there's something weird with the fromRustupToolchainFile API. The check fails to build, and it was what was wrong with the devShell

@RossSmyth
Copy link
Contributor Author

Ok with some help from oxalica I found my typo and it should work fine now.

@RossSmyth
Copy link
Contributor Author

RossSmyth commented Feb 27, 2025

An optional thing that could be done (and I do this in my repos at work) is move the mkHelix function to the default.nix file.
See: RossSmyth@bb2141c

@RossSmyth
Copy link
Contributor Author

It also looks like finalAttrs for buildRustPackage was merged, so the TODOs can be resolved.
RossSmyth@4edfad0

@RossSmyth RossSmyth requested a review from the-mikedavis March 3, 2025 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-packaging Area: Packaging and bundling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants