forked from pub-solar/os
shell: cleanup devshell
* Add update command to flk * Add cleaner usage to flk command * Factor out scripts to be more readable
This commit is contained in:
parent
1eb4d1f5ea
commit
933fb8fee9
|
@ -72,7 +72,7 @@
|
|||
});
|
||||
|
||||
devShell = import ./shell {
|
||||
inherit pkgs nixos;
|
||||
inherit self system;
|
||||
};
|
||||
|
||||
legacyPackages.hmActivationPackages =
|
||||
|
|
|
@ -1,50 +1,19 @@
|
|||
{ pkgs ? (import ./compat).defaultNix.nixosConfigurations.NixOS.config.nixpkgs.pkgs
|
||||
, nixos ? (import ./compat).defaultNix.inputs.nixos
|
||||
, ...
|
||||
{ self ? (import ../compat).defaultNix
|
||||
, system ? builtins.currentSystem
|
||||
}:
|
||||
let
|
||||
build = "config.system.build";
|
||||
pkgs = (self.lib.genPkgs { inherit self; }).${system};
|
||||
|
||||
installPkgs = (import "${nixos}/nixos" {
|
||||
configuration = { };
|
||||
system = pkgs.system;
|
||||
inherit (pkgs) lib;
|
||||
|
||||
installPkgs = (lib.nixosSystem {
|
||||
inherit system;
|
||||
modules = [ ];
|
||||
}).config.system.build;
|
||||
|
||||
flk = pkgs.writeShellScriptBin "flk" ''
|
||||
if [[ -z "$1" ]]; then
|
||||
echo "Usage: $(basename $0) [ up | get [core|community] {dest} | iso {host} | install {host} | {host} [switch|boot|test] | home {host} {user} [switch] ]"
|
||||
elif [[ "$1" == "up" ]]; then
|
||||
mkdir -p "$DEVSHELL_ROOT/up"
|
||||
hostname="$(hostname)"
|
||||
nixos-generate-config --dir "$DEVSHELL_ROOT/up/$hostname"
|
||||
echo \
|
||||
"{
|
||||
imports = [ ../up/$hostname/configuration.nix ];
|
||||
}" > "$DEVSHELL_ROOT/hosts/up-$hostname.nix"
|
||||
git add -f "$DEVSHELL_ROOT/up/$hostname"
|
||||
git add -f "$DEVSHELL_ROOT/hosts/up-$hostname.nix"
|
||||
elif [[ "$1" == "iso" ]]; then
|
||||
nix build "$DEVSHELL_ROOT#nixosConfigurations.$2.${build}.iso" "${"\${@:3}"}"
|
||||
elif [[ "$1" == "install" ]]; then
|
||||
sudo nixos-install --flake "$DEVSHELL_ROOT#$2" "${"\${@:3}"}"
|
||||
elif [[ "$1" == "home" ]]; then
|
||||
nix build "./#hmActivationPackages.$2.$3" "${"\${@:4}"}"
|
||||
if [[ "$4" == "switch" ]]; then
|
||||
./result/activate && unlink result
|
||||
fi
|
||||
elif [[ "$1" == "get" ]]; then
|
||||
if [[ "$2" == "core" || "$2" == "community" ]]; then
|
||||
nix flake new -t "github:nrdxp/nixflk/$2" "${"\${3:-flk}"}"
|
||||
else
|
||||
echo "flk get [core|community] {dest}"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
sudo nixos-rebuild --flake "$DEVSHELL_ROOT#$1" "${"\${@:2}"}"
|
||||
fi
|
||||
'';
|
||||
flk = pkgs.callPackage ./flk.nix { };
|
||||
|
||||
name = "flk";
|
||||
inherit (flk) name;
|
||||
in
|
||||
pkgs.devshell.mkShell {
|
||||
inherit name;
|
||||
|
@ -59,71 +28,31 @@ pkgs.devshell.mkShell {
|
|||
|
||||
env = { inherit name; };
|
||||
|
||||
git.hooks = with pkgs; {
|
||||
git.hooks = {
|
||||
enable = true;
|
||||
pre-commit.text = ''
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if ${git}/bin/git rev-parse --verify HEAD >/dev/null 2>&1
|
||||
then
|
||||
against=HEAD
|
||||
else
|
||||
# Initial commit: diff against an empty tree object
|
||||
against=$(${git}/bin/git hash-object -t tree /dev/null)
|
||||
fi
|
||||
|
||||
diff="${git}/bin/git diff-index --name-only --cached $against --diff-filter d"
|
||||
|
||||
nix_files=($($diff -- '*.nix'))
|
||||
|
||||
all_files=($($diff))
|
||||
|
||||
# Format staged nix files.
|
||||
${nixpkgs-fmt}/bin/nixpkgs-fmt "${"\${nix_files[@]}"}" \
|
||||
&& git add "${"\${nix_files[@]}"}"
|
||||
|
||||
# check editorconfig
|
||||
${editorconfig-checker}/bin/editorconfig-checker -- "${"\${all_files[@]}"}"
|
||||
if [[ $? != '0' ]]; then
|
||||
{
|
||||
echo -e "\nCode is not aligned with .editorconfig"
|
||||
echo "Review the output and commit your fixes"
|
||||
} >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
pre-commit.text = lib.fileContents ./pre-commit.sh;
|
||||
};
|
||||
|
||||
commands = with pkgs; [
|
||||
{
|
||||
name = nixpkgs-fmt.pname;
|
||||
package = nixpkgs-fmt;
|
||||
help = nixpkgs-fmt.meta.description;
|
||||
category = "linters";
|
||||
}
|
||||
{
|
||||
name = flk.name;
|
||||
help = "Build, deploy, and install nixflk";
|
||||
category = "main";
|
||||
package = flk;
|
||||
}
|
||||
{
|
||||
name = "grip";
|
||||
help = python38Packages.grip.meta.description;
|
||||
category = "servers";
|
||||
package = python38Packages.grip;
|
||||
}
|
||||
{
|
||||
name = git.pname;
|
||||
help = git.meta.description;
|
||||
category = "vcs";
|
||||
package = git;
|
||||
}
|
||||
commands = with pkgs; let
|
||||
mkCommand = category: package: {
|
||||
inherit package category;
|
||||
name = package.pname or package.name;
|
||||
help = package.meta.description;
|
||||
};
|
||||
|
||||
mapCmd = category: map (mkCommand category);
|
||||
in
|
||||
mapCmd "main" [ flk cachix git ] ++
|
||||
mapCmd "linters" [ nixpkgs-fmt editorconfig-checker ] ++
|
||||
mapCmd "documentation" [ python3Packages.grip mdbook ] ++ [
|
||||
{
|
||||
name = "nix";
|
||||
help = nixFlakes.meta.description;
|
||||
category = "main";
|
||||
command = ''${nixFlakes}/bin/nix --option experimental-features "nix-command flakes ca-references" "$@"'';
|
||||
command = ''
|
||||
${nixFlakes}/bin/nix --option experimental-features \
|
||||
"nix-command flakes ca-references" "$@"
|
||||
'';
|
||||
}
|
||||
];
|
||||
|
||||
|
|
23
shell/flk.nix
Normal file
23
shell/flk.nix
Normal file
|
@ -0,0 +1,23 @@
|
|||
{ stdenv }:
|
||||
let
|
||||
name = "flk";
|
||||
in
|
||||
stdenv.mkDerivation {
|
||||
inherit name;
|
||||
|
||||
src = ./flk.sh;
|
||||
|
||||
dontUnpack = true;
|
||||
dontBuild = true;
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
install $src $out/bin/${name}
|
||||
'';
|
||||
|
||||
checkPhase = ''
|
||||
${stdenv.shell} -n -O extglob $out/bin/${name}
|
||||
'';
|
||||
|
||||
meta.description = "Build, deploy, and install NixOS";
|
||||
}
|
82
shell/flk.sh
Executable file
82
shell/flk.sh
Executable file
|
@ -0,0 +1,82 @@
|
|||
[[ -d "$DEVSHELL_ROOT" ]] ||
|
||||
{
|
||||
echo "This script must be run from nixflk's devshell" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
shopt -s extglob
|
||||
|
||||
HOSTNAME="$(hostname)"
|
||||
|
||||
usage () {
|
||||
printf "%b\n" \
|
||||
"\e[4mUsage\e[0m: $(basename $0) COMMAND [ARGS]\n" \
|
||||
"\e[4mCommands\e[0m:"
|
||||
|
||||
printf " %-30s %s\n\n" \
|
||||
"up" "Generate $DEVSHELL_ROOT/hosts/up-$HOSTNAME.nix" \
|
||||
"update" "Update and commit the lock file" \
|
||||
"get [core|community] DEST" "Copy the desired template to DEST" \
|
||||
"iso HOST" "Generate an ISO image of HOST" \
|
||||
"install HOST [ARGS]" "Shortcut for nixos-install" \
|
||||
"home HOST USER [switch]" "Home-manager config of USER from HOST" \
|
||||
"HOST [switch|boot|test]" "Shortcut for nixos-rebuild"
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
""|"-h"|"help"|*(-)"help")
|
||||
usage
|
||||
;;
|
||||
|
||||
"up")
|
||||
mkdir -p "$DEVSHELL_ROOT/up"
|
||||
|
||||
nixos-generate-config --dir "$DEVSHELL_ROOT/up/$HOSTNAME"
|
||||
|
||||
printf "{ imports = [ ../up/$HOSTNAME/configuration.nix ]; }" \
|
||||
> "$DEVSHELL_ROOT/hosts/up-$HOSTNAME.nix"
|
||||
|
||||
git add -f \
|
||||
"$DEVSHELL_ROOT/up/$HOSTNAME" \
|
||||
"$DEVSHELL_ROOT/hosts/up-$HOSTNAME.nix"
|
||||
;;
|
||||
|
||||
"update")
|
||||
nix flake update --recreate-lock-file --commit-lock-file "$DEVSHELL_ROOT"
|
||||
;;
|
||||
|
||||
"get")
|
||||
if [[ "$2" == "core" || "$2" == "community" ]]; then
|
||||
nix flake new -t "github:nrdxp/nixflk/$2" "${3:-flk}"
|
||||
else
|
||||
echo "flk get [core|community] {dest}"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
|
||||
"iso")
|
||||
nix build \
|
||||
"$DEVSHELL_ROOT#nixosConfigurations.$2.config.system.build.iso" \
|
||||
"${@:3}"
|
||||
;;
|
||||
|
||||
"install")
|
||||
sudo nixos-install --flake "$DEVSHELL_ROOT#$2" "${@:3}"
|
||||
;;
|
||||
|
||||
"home")
|
||||
ref="$DEVSHELL_ROOT/#hmActivationPackages.$2.$3"
|
||||
|
||||
if [[ "$4" == "switch" ]]; then
|
||||
nix build "$ref" && result/activate &&
|
||||
unlink result
|
||||
|
||||
else
|
||||
nix build "$ref" "${@:4}"
|
||||
fi
|
||||
;;
|
||||
|
||||
*)
|
||||
sudo nixos-rebuild --flake "$DEVSHELL_ROOT#$1" "${@:2}"
|
||||
;;
|
||||
esac
|
29
shell/pre-commit.sh
Executable file
29
shell/pre-commit.sh
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
if git rev-parse --verify HEAD >/dev/null 2>&1
|
||||
then
|
||||
against=HEAD
|
||||
else
|
||||
# Initial commit: diff against an empty tree object
|
||||
against=$(${git}/bin/git hash-object -t tree /dev/null)
|
||||
fi
|
||||
|
||||
diff="git diff-index --name-only --cached $against --diff-filter d"
|
||||
|
||||
nix_files=($($diff -- '*.nix'))
|
||||
all_files=($($diff))
|
||||
|
||||
# Format staged nix files.
|
||||
if [[ -n "${nix_files[@]}" ]]; then
|
||||
nixpkgs-fmt "${nix_files[@]}" \
|
||||
&& git add "${nix_files[@]}"
|
||||
fi
|
||||
|
||||
# check editorconfig
|
||||
editorconfig-checker -- "${all_files[@]}"
|
||||
if [[ $? != '0' ]]; then
|
||||
printf "%b\n" \
|
||||
"\nCode is not aligned with .editorconfig" \
|
||||
"Review the output and commit your fixes" >&2
|
||||
exit 1
|
||||
fi
|
Loading…
Reference in a new issue