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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With