lycheeverse / lychee

⚡ Fast, async, stream-based link checker written in Rust. Finds broken URLs and mail addresses inside Markdown, HTML, reStructuredText, websites and more!
https://lychee.cli.rs
Apache License 2.0
2.22k stars 134 forks source link

Add Nix shell #1565

Open thomas-zahner opened 2 weeks ago

thomas-zahner commented 2 weeks ago

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 commented 1 week ago

From my side, this looks good.

niklaskorz commented 1 week ago

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 commented 6 days ago

@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 commented 9 hours ago

@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 commented 8 hours ago

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

thomas-zahner commented 8 hours ago

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 commented 8 hours ago

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.