Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package a single Python script with nix?

Tags:

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.

like image 456
nh2 Avatar asked May 07 '17 23:05

nh2


2 Answers

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 unpackPhase = "true"; (this is /bin/true, the program that does nothing).

myscript-package = pkgs.stdenv.mkDerivation {
  name = "myscript";
  buildInputs = [
    (pkgs.python36.withPackages (pythonPackages: with pythonPackages; [
      consul
      six
      requests2
    ]))
  ];
  unpackPhase = "true";
  installPhase = ''
    mkdir -p $out/bin
    cp ${./myscript.py} $out/bin/myscript
    chmod +x $out/bin/myscript
  '';
};

If your script is executable (which we ensure with chmod +x 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 buildInputs 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.

like image 72
nh2 Avatar answered Nov 09 '22 02:11

nh2


This helper function is really nice:

pkgs.writers.writePython3Bin "github-owner-repos" { libraries = [ pkgs.python3Packages.PyGithub ]; } ''
 import os
 import sys
 from github import Github

 if __name__ == '__main__':
     gh = Github(os.environ['GITHUB_TOKEN'])

     for repo in gh.get_user(login=sys.argv[1]).get_repos():
         print(repo.ssh_url)
''

https://github.com/nixos/nixpkgs/blob/master/pkgs/build-support/writers/default.nix#L319

like image 4
Peter Esselius Avatar answered Nov 09 '22 02:11

Peter Esselius