Basic flake: run existing (python, bash) script

writePython3Bin seems like the way to accomplish #4:

An example including dependencies:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      system = "aarch64-darwin";
      pkgs = nixpkgs.legacyPackages.${system};
      py3pkgs = pkgs.python3.pkgs;
    in
    {
      packages.${system}.default = pkgs.writers.writePython3Bin "say_foo" { libraries = [ py3pkgs.requests ]; } ''
        import requests
        status = requests.get("https://n8henrie.com").status_code
        print(status)
      '';
    };
}
$ NIXPKGS_ALLOW_BROKEN=1 nix run --impure
200

Accomplishing #5 and #6 seem pretty simple leveraging the same builtins.readFile as above:

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

  outputs = { self, nixpkgs }:
    let
      system = "aarch64-darwin";
      pkgs = nixpkgs.legacyPackages.${system};
      py3pkgs = pkgs.python3.pkgs;
    in
    {
      packages.${system}.default = pkgs.writers.writePython3Bin "say_foo"
        {
          libraries = [ py3pkgs.requests ];
        } "${builtins.readFile ./foo.py}";
    };
}
# foo.py
import sys

import requests

print(sys.executable)
status = requests.get("https://n8henrie.com").status_code
print(status)
$ NIXPKGS_ALLOW_BROKEN=1 nix run --impure
/nix/store/gg1vps3aljdismx7rqps4m738gjcf9fp-python3-3.10.4-env/bin/python3.10
200

I guess the only part remaining is the “arbitrary PyPI dependency part.”

2 Likes