Skip to content

Commit

Permalink
build(bin): rewrite up in shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Oct 29, 2023
1 parent f52ef8a commit 69d2a27
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 99 deletions.
8 changes: 0 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,3 @@ indent_size = 2

[*.{yml,nix}]
indent_style = space

[*.py]
indent_style = space
indent_size = 4

[bin/up]
indent_style = space
indent_size = 4
128 changes: 37 additions & 91 deletions bin/up
Original file line number Diff line number Diff line change
@@ -1,91 +1,37 @@
#!/usr/bin/env python3

import argparse
import subprocess
import pathlib
import sys

DIR = pathlib.Path.home().joinpath('.dotfiles')


def run(command):
try:
r = subprocess.run(
command,
check=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True
)
return r.stdout
except Exception as error:
print('failed with the following error:', '\n')

if isinstance(error, subprocess.CalledProcessError):
print(error.stderr)
else:
print(error)

exit(1)


def run_nix(sub_command):
run(["nix", "--extra-experimental-features",
"nix-command flakes", *sub_command, DIR])

def switch():
print("switching configuration ...")
cmd = ["sudo", "nixos-rebuild"]

if sys.platform == "darwin":
cmd = ["darwin-rebuild"]
boot_loader = []

run([*cmd, "switch", "--flake", DIR])

def test():
print("(test) applying configurations ...")
cmd = ["sudo", "nixos-rebuild"]

if sys.platform == "darwin":
cmd = ["darwin-rebuild"]

run([*cmd, "test", "--fast", "--flake", DIR])


def main():
parser = argparse.ArgumentParser(
prog="up",
description="easily apply flake-based system changes."
)

subparsers = parser.add_subparsers(metavar="", dest="command")
subparsers.add_parser('boot', help='switch and install boot loader')
subparsers.add_parser('pull', help='update flake inputs')
subparsers.add_parser('test', help='quickly test configurations')
subparsers.add_parser('revert', help='roll back to previous generation')
subparsers.add_parser('prune', help='run garbage collection')
subparsers.add_parser(
'theme',
help='switch the theme of all supported apps'
)

args = parser.parse_args()

if args.command == None:
switch()
elif args.command == "pull":
print("updating flake inputs ...")
run_nix(["flake", "update"])
elif args.command == "test":
test()
elif args.command == "revert":
print("Not implemented")
elif args.command == "prune":
run(["sudo", "nix-collect-garbage"])
elif args.command == "theme":
print("Not implemented")


if __name__ == '__main__':
main()
#!/usr/bin/env sh

set -euo pipefail

src="$HOME/.dotfiles"

nix() {
command nix --experimental-features 'nix-command flakes' $@
}

flake() {
if
command -v nixos-rebuild >/dev/null 2>&1
then
sudo nixos-rebuild $@ --flake $src
else
nix run nix-darwin -- $@ --flake $src
fi
}

case ${1-default} in
build)
flake build
;;
switch)
flake switch
;;
test)
flake test --fast
;;
prune)
nix-collect-garbage
;;
*)
nix flake update $src
;;
esac

0 comments on commit 69d2a27

Please sign in to comment.