Created
May 26, 2018 17:39
-
-
Save krey/348c9a3efdc33512640d62fa7e5674bb to your computer and use it in GitHub Desktop.
nix channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "nixos", | |
"repo": "NixOS/nixpkgs-channels", | |
"branch": "nixos-18.03" | |
}, | |
{ | |
"name": "nixos-unstable", | |
"repo": "NixOS/nixpkgs-channels", | |
"branch": "nixos-unstable" | |
}, | |
{ | |
"name": "nixpkgs-master", | |
"repo": "NixOS/nixpkgs" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "nixos", | |
"repo": "NixOS/nixpkgs-channels", | |
"branch": "nixos-18.03", | |
"rev": "2f6440eb09b7e6e3322720ac91ce7e2cdeb413f9" | |
}, | |
{ | |
"name": "nixos-unstable", | |
"repo": "NixOS/nixpkgs-channels", | |
"branch": "nixos-unstable", | |
"rev": "5da85431fb1df4fb3ac36730b2591ccc9bdf5c21" | |
}, | |
{ | |
"name": "nixpkgs-master", | |
"repo": "NixOS/nixpkgs", | |
"rev": "97e376bf9cafa2d6c812221677f2e38163d0acb8" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env nix-shell | |
#! nix-shell -I "nixpkgs=https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz" -i python3 -p python3 | |
##! nix-shell -i python3 -p python3 | |
import json | |
import urllib.request | |
import urllib.error | |
import tempfile | |
import os | |
import os.path | |
import tarfile | |
import shutil | |
import subprocess | |
import sys | |
channels_json = "channels.json" | |
channels_pinned_json = "channels.pinned.json" | |
channels_dir = "/nix/var/nix/profiles/per-user/root/channels" | |
# Github api | |
commit_url_template = 'https://api.github.com/repos/{repo}/commits/{ref}' | |
tarball_url_template = 'https://api.github.com/repos/{repo}/tarball/{ref}' | |
def read_url(url): | |
try: | |
with urllib.request.urlopen(url) as response: | |
return response.read() | |
except urllib.error.HTTPError: | |
print(url) | |
raise | |
def save_url(url): | |
try: | |
return urllib.request.urlretrieve(url)[0] | |
except urllib.error.HTTPError: | |
print(url) | |
raise | |
def pin(): | |
with open(channels_json) as f: | |
channels = json.load(f) | |
print("Pinning channels") | |
for channel in channels: | |
print("channel: "+channel['name']) | |
if not 'rev' in channel: | |
ref = channel.get('branch', 'HEAD') | |
url = commit_url_template.format(repo=channel['repo'], ref=ref) | |
channel['rev'] = json.loads(read_url(url))['sha'] | |
with open(channels_pinned_json, 'w') as f: | |
json.dump(channels, f, indent=4) | |
def update(usePython=False): | |
with open(channels_pinned_json) as f: | |
channels = json.load(f) | |
print("Updating channels") | |
for channel in channels: | |
channel_dir = os.path.join(channels_dir, channel['name']) | |
print("channel: "+channel['name']) | |
with tempfile.NamedTemporaryFile() as temp_file: | |
print("- downloading") | |
temp_file = save_url(tarball_url_template.format(repo=channel['repo'], ref=channel['rev'])) | |
with tempfile.TemporaryDirectory() as temp_dir: | |
print("- extracting") | |
subprocess.run(["tar", "xzf", temp_file, "-C", temp_dir], check=True) | |
if os.path.isdir(channel_dir): | |
print("- cleaning") | |
subprocess.run(["rm", "-r", channel_dir], check=True) | |
print("- moving") | |
subprocess.run(["mv", os.path.join(temp_dir, os.listdir(temp_dir)[0]), channel_dir], check=True) | |
def clean(): | |
print("Cleaning all channels") | |
subprocess.run(["rm", "-r", channels_dir], check=True) | |
subprocess.run(["mkdir", channels_dir], check=True) | |
if __name__ == '__main__': | |
if 'clean' in sys.argv[1:]: | |
clean() | |
if 'pin' in sys.argv[1:]: | |
pin() | |
if 'update' in sys.argv[1:]: | |
update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment