Installing libusb1 on nixos

I installed libusb on nixos which is dependent on the hidapi library. I installed the library by adding the following to my /etc/nixos/configuration.nix file and running sudo nixos-rebuild switch .

  environment.systemPackages = [
    pkgs.libusb1    
  ];

However I have tried all the languages that implement the libusb api including C, lua, python and now rust but I cannot seem to get the libusb libary installed and have the .so exposed globalling for these langauges to pickup to experiment. What is the best practice here? Create a default.nix? and include the required packages and launch a nix shell with nix develop ? If so is there a template or an automated manner of doing such a thing. Thank you. Any help would be greatly appreciated. See Screenshot below.

You can not install libraries globally with nixos, this is by design. There are in theory ways around that, but we won’t get into them.

Instead:

  • For local development prepare yourself a development environment that you use with nix-shell/nix develop. Just throw your required libs and often also pkg-config into the packages list of the mkShell call.
  • For package development add it to the buildInputs, remember that some “discovery mechanisms” rely on pkg-config being in the nativeBuildInputs. Other though require manual intervention and modification of make vars, env vars or patching of files even.

I’m on the fence with “generalized” templates. Every project is different, and as such requires a slightly different set of libraries. Therefore I write the devenvs from scratch each time.

2 Likes

@jedimaster the nix way to do this is to do something like nix-shell -p pkg-config libusb1 hidapi

Most people will put this into a shell.nix or the devShell atribute of a flake.nix. This allows for you to have per-project dev shells.

Also, my perspective is that you never “install” anything with nix, just expose it in different ways. Unfortunately, “installing software” is what people are used to, and it’s the first thing you need to unlearn to leverage nix well :slight_smile:

3 Likes

@jonringer Thank you very much. I got it to work.
Here is the default.nix I used for reference.

 { pkgs ? import <nixpkgs> { } }:
let
  dependencies = with pkgs; [
    pkg-config
    libusb1
    hidapi
  ];
in
with pkgs;
mkShell {

  dependencies = dependencies;

  packages = [
    dependencies
    # cowsay 
  ];

  buildInputs = [
    dependencies
  ];

  nativeBuildInputs = [
    pkgs.libusb1
  ];
}

@NobbZ Thank you for the insight.

For some unsolicited commentary:

This should be achievable with just:

 { pkgs ? import <nixpkgs> { } }:

with pkgs;
mkShell {
  nativeBuildInputs = [
    pkg-config
  ];

  buildInputs = [
    hidapi
    libusb1
  ];

}

with pkgs; is a “smell”, but for something short like this, I think it’s reasonable.

May want to look at Nix - Flakes for out-of-tree code - YouTube as well :slight_smile: