How to replicate libfl-dev in nix?

Hey there, I’m fairly new to Nix(OS) and I started using it at work too (using nix on WSL). Today I wanted to compile some C code and I’ve been hit with a missing libfl. Using libfl-dev I could easily solve out the problem but I tried to do it the nix way and got pretty stuck.

Only related thing I could found was flex does not install libfl.{a,so} · Issue #40918 · NixOS/nixpkgs · GitHub but when I try to nix-shell -p flex I only get:

ldd $(which flex)
linux-vdso.so.1 (0x00007f668575c000)
libm.so.6 => /nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib/libm.so.6 (0x00007f6685670000)
libc.so.6 => /nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib/libc.so.6 (0x00007f6685479000)
/nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib/ld-linux-x86-64.so.2 => /nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib64/ld-linux-x86-64.so.2 (0x00007f668575e000)

Any help is greatly appreciated!

By running nix-build '<nixpkgs>' -A flex you’ll get the directory of the flex package (It also creates a symlink called result for easier access)
I was able to find libfl.so inside the lib directory.

You can link the libfl library by using -lfl.
Though, I haven’t used flex yet, so if you still have issues, please provide some code.

I can confirm that nix-build '<nixpkgs>' -A flex and looking into the resulted folder /lib does provide that libfl.so. Though I don’t understand how I should use -lfl and how can I use this directly in a nix-shell.

I’m guessing after you’ve generated the .yy.cc file or something like that using the flex command, you still have to compile it using gcc or g++ passing the -lfl flag. Though, that might be optional, I don’t know.

As you have not shared what you’re trying to do exactly I can only guess.

I’m running a make all command. Would provide more details but sadly the repo I’m working on is private.

An error message would be useful.

If it’s just a missing link, an hacky solution would be to do export NIX_LDFLAGS="$NIX_LDFLAGS -lfl" which should force-link libfl.

This is the error after running make all in nix-shell -p flex

error while loading shared libraries: libfl.so.2: cannot open shared object file: No such file or directory

In the end I got it with the following shell.nix:

{
  pkgs ? import <nixpkgs> { },
}:
pkgs.mkShell {
  buildInputs = [
    pkgs.flex
  ];
  nativeBuildInputs = [
    pkgs.pkg-config
  ];
  LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [ pkgs.flex ];
  shellHook = ''
    echo hello
  '';
}

The key being that makeLibraryPath.