<<< Back to list

TIL: Use FHS in a nix flake developer shell

At work, we’ve got some websites that haven’t been updated in some time and use some.. pretty old packages with native dependencies. With my main development machine running NixOS it’s been a challenge to build these packages because they make all sorts of assumptions about how the filesystem is laid out. In order to stay efficient and get on with my job, I resorted to running these websites inside a Ubuntu lxc container with a shared directory. This solved the immediate problem, I could work on these websites, but it felt like a workaround.

Today I learned that I could achieve the same effect by creating a FHS environment using buildFHSUserEnv. A bit of trial and error later and I managed to make it work through nix develop too. Here’s a simple flake.nix:

# flake.nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
      fhs = pkgs.buildFHSUserEnv {
        name = "fhs-shell";
        targetPkgs = pkgs: [pkgs.gcc pkgs.libtool pkgs.nodejs-18_x] ;
      };
    in
      {
        devShells.${system}.default = fhs.env;
      };
}

Now I can access this environment through nix develop. The only thing to be aware is that the derivation that buildFHSUserEnv makes is exposed through the .env attribute and that is what needs to be exported as a dev shell.

You can read more about buildFHSUserEnv here.