MS SQL Tools (bcp) attempt

I am totally new to nix, and I had an immediate need for Microsoft’s bcp utility. I built the following derivation:

{ lib, stdenv, fetchurl, dpkg, patchelf, unixODBC, ... }:
stdenv.mkDerivation rec {
  pname = "mssql-bcp";
  name = "mssql-bcp";
  version = "18.2.1.1-1";
  src = fetchurl {
    url = "https://packages.microsoft.com/debian/11/prod/pool/main/m/mssql-tools18/mssql-tools18_${version}_amd64.deb";
    sha256 = "70f219b0d7a4d4a9ff3596164a9018bf5bbc61d4313c83186010f81b3f292218";
  };
  nativeBuildInputs = [ dpkg patchelf ];

  unpackPhase = ''
      dpkg -x $src ./
    '';

  installPhase = ''
    mkdir -p $out/bin
    cp opt/mssql-tools18/bin/bcp $out/bin/bcp
    cp -r opt/mssql-tools18/share $out/
  '';

  postFixup = ''
    patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out/bin/bcp
    patchelf --set-rpath ${lib.makeLibraryPath [ unixODBC stdenv.cc.cc ]} $out/bin/bcp
  '';

  meta = {
    description = "Microsoft SQL Server command-line tool bcp";
    license = lib.licenses.unfree;
    maintainers = [ {
      email = "git@bowmanjd.org";
      github = "bowmanjd";
      githubId = 86415;
      name = "Jonathan Bowman";
    } ];
  };
}

It depends on unixODBCDrivers.msodbcsql18.

Thing is, it only works if I add this to configuration.nix:

environment.unixODBCDrivers = with pkgs.unixODBCDrivers; [ msodbcsql18 ];

In other words, that final configuration is a dependency for the package; it is broken without it. Given this, is there any way I should be incorporating this requirement into the derivation?

I also welcome general feedback on the derivation.

People usually make a small module in cases like this, and you’d use programs.bcp.enable to install it instead of adding the package to anything.

Given that option just writes a small .ini into /etc, I suppose you might be able to make something work with buildFhsUserEnv or something like that instead, if there’s a good reason to keep the configuration independent from the system config. Or maybe you can find an undocumented environment variable that lets you change the default path on that and then use a wrapper or something.

1 Like

Well, yes. There is the variable ODBCINST, I believe. Great idea. Is there a way to incorporate an environment variable into a derivation?

The module idea is brilliant, too. That makes a lot of sense. Thank you!

That’s what makeWrapper and its derivatives are for.

1 Like