-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(bin): rewrite up in shell script
- Loading branch information
Showing
2 changed files
with
37 additions
and
99 deletions.
There are no files selected for viewing
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
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
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 |