|
| 1 | +#!/usr/bin/env nix-shell |
| 2 | +#! nix-shell -i python3 -p "python311.withPackages (ps: with ps; [ click ])" |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | + |
| 11 | +def get_nixpkgs_path(path): |
| 12 | + result = subprocess.run( |
| 13 | + ["nix", "eval", "-f", path, "inputs.nixpkgs.outPath", "--json"], |
| 14 | + capture_output=True, |
| 15 | + text=True, |
| 16 | + ) |
| 17 | + result.check_returncode() |
| 18 | + return json.loads(result.stdout) |
| 19 | + |
| 20 | + |
| 21 | +def resolve_nixpkgs(nixpkgs, path): |
| 22 | + return nixpkgs if nixpkgs else get_nixpkgs_path(path) |
| 23 | + |
| 24 | + |
| 25 | +def build_nix_args(update_nix, path, attr_path, commit): |
| 26 | + args = [ |
| 27 | + update_nix, |
| 28 | + "--arg", |
| 29 | + "include-overlays", |
| 30 | + f"[(import {path}).overlays.default]", |
| 31 | + "--argstr", |
| 32 | + "path", |
| 33 | + attr_path, |
| 34 | + ] |
| 35 | + if commit: |
| 36 | + args.append("--argstr", "commit", "true") |
| 37 | + return args |
| 38 | + |
| 39 | + |
| 40 | +@click.command() |
| 41 | +@click.option("--commit", is_flag=True, help="Commit the changes") |
| 42 | +@click.option( |
| 43 | + "--nixpkgs", |
| 44 | + help="Override the nixpkgs flake input with this path, it will be used for finding update.nix", |
| 45 | + default=None, |
| 46 | +) |
| 47 | +@click.argument("attr_path") |
| 48 | +def main(commit, nixpkgs, attr_path): |
| 49 | + path = os.getcwd() |
| 50 | + nixpkgs_path = resolve_nixpkgs(nixpkgs, path) |
| 51 | + update_nix_bin = os.path.join(nixpkgs_path, "maintainers/scripts/update.nix") |
| 52 | + nix_args = build_nix_args(update_nix_bin, path, attr_path, commit) |
| 53 | + os.execvp("nix-shell", ["nix-shell"] + nix_args) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments