Skip to content

Instantly share code, notes, and snippets.

@garbas
Last active May 18, 2016 18:56
Show Gist options
  • Save garbas/991aef248091f0692d3b to your computer and use it in GitHub Desktop.
Save garbas/991aef248091f0692d3b to your computer and use it in GitHub Desktop.
nix porcelain
#!/usr/bin/env nix-shell
#!nix-shell -i python -p pythonPackages.click
import click
import shlex
import subprocess
# helper methods
def run(command):
if isinstance(command, basestring):
command = shlex.split(command)
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
out = []
while True:
line = p.stdout.readline()
if line == '' and p.poll() is not None:
break
if line != '':
click.secho(line.rstrip('\n'))
out.append(line)
return p.returncode
# commands
@click.group()
def main():
"""Nix package manager.
Proposal to impove UX of nix-* command line tools.
More documentation can be found at:
https://nixos.org/nix/manual
"""
@main.command()
@click.argument('pkg', type=str)
def install(pkg):
"""Install package.
"""
run('nix-env -i %s' % pkg)
@main.command()
@click.argument('pkg', type=str)
def uninstall(pkg):
"""Uninstall package.
"""
run('nix-env -e %s' % pkg)
@main.command()
@click.argument('pkg', default=None, required=False, type=str)
def search(pkg):
"""Search available packages.
"""
if pkg:
pkg = '".*%s.*"' % pkg
else:
pkg = ''
run('nix-env -qaP ".*%s.*"' % pkg)
@main.command()
@click.argument('pkg', default=None, required=False, type=str)
def list(pkg):
"""List currently installed packages.
"""
if pkg:
pkg = '".*%s.*"' % pkg
else:
pkg = ''
run('nix-env -q --installed %s' % pkg)
@main.command()
@click.argument('pkg', default=None, required=False, type=str)
def upgrade(pkg):
"""Upgrade package.
"""
if not pkg:
pkg = ''
run('nix-env -u %s' % pkg)
if __name__ == "__main__":
main()
@garbas
Copy link
Author

garbas commented Nov 19, 2015

just before somebody starts screaming at me ... nix is not going to be rewritten in python. this is just a prototype to play with the ux/ui of the cli tool. once we figure out what we want it is going to be written in C++.

@unode
Copy link

unode commented May 18, 2016

Possible improvements:

  • nix search: two columns are shown but there's no header. It's not obvious what's the difference. I.e. when installing a package which one should be used?
  • nix upgrade: If no "pkg" is given, prompt the user before upgrading all packages. Something like "No package was provided, upgrade all installed packages? [Y/n] ". To override the prompt include an option "--yes".

Additional options:

  • nix channel: This was not included. Make it a sub command. (ex: nix channel add http://newchannel). Most of nix-channel commands should translate nicely (i.e. nix channel list, nix channel remove, ...)
  • nix package: Merge nix-build, nix-pull, nix-push under one name . Somewhat low-level interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment