How to update a `shell.nix` development environment to work with other systems

Hello, I have a shell.nix (https://github.com/vrrb-io/vrrb/blob/053a2abd4b0f163ef436b40f3d1a7a9751cd2106/shell.nix) that works fine on my NixOS machine, but I get error: linking with cc failed: exit code 1 when using it on Pop!_OS live boot of that same machine as well as on my M1 MBP.

Does anyone know what I should change to make the nix-shell work on these systems?

Thanks!

Some things I’ve tried:

  • adding libiconv to the buildInputs
  • removing the .rustup folder

I also have a StackOverflow post with some more info: rust - How can I update `nix-shell` development environment to work on systems other than NixOS? - Stack Overflow

the macos error looks like it is missing darwin.apple_sdk.frameworks.Security in buildInputs, It’s hard to tell from the error message what the pop os build is missing

1 Like

Is there a way to specify buildInputs for specific systems? That way I don’t have to include that specific input for all possible systems

Yes, buildInputs is simply a list of packages. You can put platform dependent dependencies in a lib.optionals stdenv.isDarwin [ ... ] and concat them with ++

Like this?

buildInputs = with pkgs; [
    # dev tools
    which
    htop
    zlib

    # build dependencies
    clang
    libclang.lib
    rocksdb
    openssl.dev
    pkg-config
    rustup
  ]
  ++
  lib.optionals stdenv.isDarwin [libiconv, darwin.apple_sdk.frameworks.Security];

Also do I need to change the way I’m exporting PATH in the shellHook to be the same as each system?

the list syntax is off, there shouldn’t be a , after libiconv, and some stuff like pkg-config should be in nativeBuildInputs instead. Other than that lgtm

What else would you recommend putting in nativeBuildInputs? I’m using this as a reference: Use `buildInputs` or nativeBuildInputs` for `nix-shell`? but still not quite sure if it’s of consequence since it seems to work fine on NixOS and my M1 now

In a regular derivation, which htop clang rustup would probably also go into nativeBuildIputs, but I’m not sure that it matters that much in a mkShell

1 Like

Is there a way to get the system kind and use that in the shellHook?

Right now I have it as:

export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/

but that doesn’t take into account when the system is Darwin… It seems to work but I’m curious if there’s a way to explicitly store that information so that I can make it dynamic and not mess up any path stuff

tbh I just use fenix, you can use fenix and add fenix.stable.defaultToolchain in nativeBuildInputs

1 Like

oh nice, I see you maintain that repo!

I’ll have to test that out, not sure how I would use that for this case

funny enough, this was recommended to me over rustup in the stack overflow thread as well

So if I’m already importing nix pkgs like:
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-22.11.tar.gz") {} }: so that I can use rocksdb and openssl.dev in the buildInputs, how would I import fenix to use the way you’re describing?

You can add

let
  fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { };
in

to your file and then add either (fenix.fromToolchainFile { dir = ./.; }) or fenix.stable.defaultToolchain to your nativeBuildInputs

1 Like

Once again thank you for your help, here is my current commit of the shell now using fenix

Will test it on the Pop!_OS live boot, but it is working on my M1 now. Much appreciated!

1 Like

Looks like it also works on Pop!_OS except that the openSSL libraries are having trouble linking unlike on MacOS or NixOS… Going to test on Ubuntu live environment as well and open another ticket for the openSSL issue

you might need to add something like LD_LIBRARY_PATH = lib.makeLibraryPath [ openssl ]; to your shell.nix