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

Add Nix shell #1565

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

Conversation

thomas-zahner
Copy link
Member

So I've installed Nix OS a week ago. Because of this I created this Nix shell. This way it should be possible to reliably build and develop lychee on any Nix system. I've taken the Custom Rust version example from Nix's documentation and simply added pkg-config and openssl. I'm not proficient at Nix so I'm not sure if this follows best practices. But I know it works on my system and since it's Nix it should basically work on any system :)

@niklaskorz what do you think about this.

@mre
Copy link
Member

mre commented Nov 12, 2024

From my side, this looks good.

@niklaskorz
Copy link

niklaskorz commented Nov 13, 2024

Sorry that we didn't get to go through it during RustLab anymore. A few possible improvements:

  1. Instead of setting rust-overlay, nixpkgs and other parameters as variables using let, make shell.nix a function, taking rust-overlay and pkgs as parameters that default to the values currently set in the variables. This makes them easier to override.
    {
      rust-overlay ? import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"),
      pkgs = import <nixpkgs> { overlays = [ rust-overlay ]; },
      rustVersion ? "latest", # using a specific version: "1.62.0"
      rust ? pkgs.rust-bin.stable.${rustVersion}.default.override {
        extensions = [
          "rust-src" # for rust-analyzer
          "rust-analyzer" # usable by IDEs like zed-editor
        ];
      },
    }:
    pkgs.mkShell {
      # ...
    }
  2. fetchTarball should be combined with a pinned version and hash to make the result reproducible, e.g.
    rust-overlay ? import (fetchTarball {
      url = "https://github.com/oxalica/rust-overlay/archive/23c4b3ba5f806fcf25d5a3b6b54fa0d07854c032.tar.gz";
      sha256 = "1iy52ms10dcp90r046ida0albxy0qkfk5yhc2ijp4r02hyd63rfr";
    }),
  3. You may want to consider using some kind of dependency management tool to pin nixpkgs to a specific commit and to make it easier to update rust-overlay without having to manually update the commit and hash. This can be achieved through npins or through the experimental flakes feature. For the beginning it's fine to skip this step though!

@thomas-zahner
Copy link
Member Author

thomas-zahner commented Nov 18, 2024

@niklaskorz No problem, thank you very much for the detailed comment. I will try to apply your suggestions and merge it this weekend, as I will be attending a local Nix meetup

@thomas-zahner
Copy link
Member Author

thomas-zahner commented Nov 24, 2024

@niklaskorz @mre I'm now using flakes as they were also suggested by colleagues at the Nix meetup. The Rust version can be easily changed in the variable and it works flawlessly on my side. So if you have no more objections, I will merge this.

@mre
Copy link
Member

mre commented Nov 24, 2024

Looks good on my end, but I have no idea of Nix.

@thomas-zahner
Copy link
Member Author

For info, this config is only working for x86. We should be able to make it compatible with other platforms, for example by using nix-systems. But for now this should suffice.

@niklaskorz
Copy link

niklaskorz commented Nov 24, 2024

For info, this config is only working for x86. We should be able to make it compatible with other platforms, for example by using nix-systems. But for now this should suffice.

Supporting multiple systems without external dependencies like flake-utils or nix-systems isn't too hard either, see e.g. https://github.com/niklaskorz/rustlab2024-wgpu/blob/ch1/flake.nix

Edit: Also, it's easy to support both experimental flakes and stable non-flakes users by just importing shell.nix into your flake.nix. Or to do it the other way around, you can use https://github.com/edolstra/flake-compat which implements flakes in a backwards-compatible way for users that don't have the experimental flakes feature enabled.

@mre
Copy link
Member

mre commented Nov 27, 2024

@thomas-zahner, I talked to @tirimia about Nix and that there's a PR for lychee. He took a look and created another PR. Maybe you can check it out and see which one we're going to merge in the end. 😉 #1575

@thomas-zahner thomas-zahner mentioned this pull request Nov 30, 2024
@thomas-zahner
Copy link
Member Author

@tirimia Thanks for you suggestions on your PR. I agree that the .envrc could potentially be annoying/unwanted. So I've now removed it and also made the flake compatible with other platforms as suggested also by @niklaskorz.

I have also tried to add a build config but didn't succeed yet. If you want you could to add it via PR, that would be great. If you have any other suggestions on this PR, please let my know. Otherwise I'll merge these changes.

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.

4 participants