Search code examples
nix

How to package a single Python script with nix?


I have a single Python script called myscript.py and would like to package it up as a nix derivation with mkDerivation.

The only requirement is that my Python script has a run-time dependency, say, for the consul Python library (which itself depends on the requests and six Python libraries).

For example for myscript.py:

#!/usr/bin/env python3

import consul
print('hi')

How to do that?

I can't figure out how to pass mkDerivation a single script (its src seems to always want a directory, or fetchgit or similar), and also can't figure out how to make the dependency libraries available at runtime.


Solution

  • When you have a single Python file as your script, you don't need src in your mkDerivation and you also don't need to unpack any source code.

    The default mkDerivation will try to unpack your source code; to prevent that, simply set dontUnpack = true.

    myscript-package = pkgs.stdenv.mkDerivation {
      name = "myscript";
      propagatedBuildInputs = [
        (pkgs.python3.withPackages (pythonPackages: with pythonPackages; [
          consul
          six
          requests2
        ]))
      ];
      dontUnpack = true;
      installPhase = "install -Dm755 ${./myscript.py} $out/bin/myscript";
    };
    

    If your script is executable (which we ensure with install -m above) Nix will automatically replace your #!/usr/bin/env python3 line with one which invokes the right specific python interpreter (the one for python36 in the example above), and which does so in an environment that has the Python packages you've specifified in propagatedBuildInputs available.

    If you use NixOS, you can then also put your package into environment.systemPackages, and myscript will be available in shells on that NixOS.