From b4ce2478b6986693e9f1c8d0cf8e40f19765eeff Mon Sep 17 00:00:00 2001 From: Lily Foster Date: Mon, 8 May 2023 19:50:00 -0400 Subject: [PATCH] nodePackages: add script to remove attrs for aliases Regenerating the entire package set takes ages and editing the file by hand is difficult. Instead we provide a script to remove packages without regenerating to make it easy. --- pkgs/development/node-packages/aliases.nix | 4 ++ pkgs/development/node-packages/remove-attr.py | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 pkgs/development/node-packages/remove-attr.py diff --git a/pkgs/development/node-packages/aliases.nix b/pkgs/development/node-packages/aliases.nix index 6bf783185d8a..4db26a9e3bee 100644 --- a/pkgs/development/node-packages/aliases.nix +++ b/pkgs/development/node-packages/aliases.nix @@ -1,6 +1,10 @@ pkgs: lib: self: super: ### Deprecated aliases - for backward compatibility +### +### !!! NOTE !!! +### Use `./remove-attr.py [attrname]` in this directory to remove your alias +### from the `nodePackages` set without regenerating the entire file. with self; diff --git a/pkgs/development/node-packages/remove-attr.py b/pkgs/development/node-packages/remove-attr.py new file mode 100755 index 000000000000..d0f95e3e63a9 --- /dev/null +++ b/pkgs/development/node-packages/remove-attr.py @@ -0,0 +1,54 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python3 -p + +import collections.abc +import fileinput +import json +import os.path +import re +import sys + + +def remove(attr): + with open(os.path.join(os.path.dirname(__file__), 'node-packages.json'), 'r+') as node_packages_json: + packages = json.load(node_packages_json) + idx = 0 + while idx < len(packages): + if packages[idx] == attr or (isinstance(packages[idx], collections.abc.Mapping) and next(iter(packages[idx].keys())) == attr): + del packages[idx] + else: + idx += 1 + + node_packages_json.seek(0) + for idx, package in enumerate(packages): + if idx == 0: + node_packages_json.write('[\n ') + else: + node_packages_json.write(', ') + json.dump(package, node_packages_json) + node_packages_json.write('\n') + node_packages_json.write(']\n') + node_packages_json.truncate() + + with fileinput.input(os.path.join(os.path.dirname(__file__), 'node-packages.nix'), inplace=1) as node_packages: + safe_attr = re.escape(attr) + in_attr = False + for line in node_packages: + if in_attr: + if re.fullmatch(r' \};\n', line): + in_attr = False + else: + if re.fullmatch(rf' (?:{safe_attr}|"{safe_attr}") = nodeEnv\.buildNodePackage \{{\n', line): + in_attr = True + else: + sys.stdout.write(line) + + +if __name__ == '__main__': + import argparse + + parser = argparse.ArgumentParser(description='Remove a given package from the node-packages.nix file') + parser.add_argument('attr', help='The package attribute to remove') + args = parser.parse_args() + + remove(args.attr)