From f5d82afca784273ceb404211b8aa035f444c1af5 Mon Sep 17 00:00:00 2001 From: GTrunSec Date: Sat, 2 Apr 2022 15:40:44 -0700 Subject: [PATCH] add treefmt --- compat.nix | 6 -- default.nix | 16 +++- devshell/default.nix | 15 +++ devshell/devshell.toml | 16 +--- flake.lock | 39 ++++---- flake.nix | 7 ++ .../cluster/kubernetes/mk-docker-opts.sh | 92 +++++++++---------- shell.nix | 16 +++- tests/k8s/deployment.yaml | 8 +- tests/k8s/pod.json | 10 +- treefmt.toml | 25 +++++ 11 files changed, 147 insertions(+), 103 deletions(-) delete mode 100644 compat.nix create mode 100644 treefmt.toml diff --git a/compat.nix b/compat.nix deleted file mode 100644 index 29e4708..0000000 --- a/compat.nix +++ /dev/null @@ -1,6 +0,0 @@ -{ - flake-compat = import (builtins.fetchurl { - url = "https://raw.githubusercontent.com/edolstra/flake-compat/99f1c2157fba4bfe6211a321fd0ee43199025dbf/default.nix"; - sha256 = "1vas5z58901gavy5d53n1ima482yvly405jp9l8g07nr4abmzsyb"; - }); -} diff --git a/default.nix b/default.nix index 94703b2..9d45397 100644 --- a/default.nix +++ b/default.nix @@ -1,8 +1,14 @@ -{system ? builtins.currentSystem}: ( - (import ./compat.nix).flake-compat { - src = ./.; - inherit system; - } + import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${ + lock.nodes.flake-compat.locked.rev + }.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) {src = ./.;} ) .defaultNix diff --git a/devshell/default.nix b/devshell/default.nix index fff4d33..4929056 100644 --- a/devshell/default.nix +++ b/devshell/default.nix @@ -4,4 +4,19 @@ }: pkgs.devshell.mkShell { imports = [(pkgs.devshell.importTOML ./devshell.toml)]; + + packages = [ + pkgs.shfmt + pkgs.nodePackages.prettier + pkgs.nodePackages.prettier-plugin-toml + pkgs.alejandra + ]; + commands = [ + { + package = pkgs.treefmt; + } + ]; + devshell.startup.nodejs-setuphook = pkgs.lib.stringsWithDeps.noDepEntry '' + export NODE_PATH=${pkgs.nodePackages.prettier-plugin-toml}/lib/node_modules:$NODE_PATH + ''; } diff --git a/devshell/devshell.toml b/devshell/devshell.toml index 326b662..87d3828 100644 --- a/devshell/devshell.toml +++ b/devshell/devshell.toml @@ -8,20 +8,7 @@ eval = "$DEVSHELL_ROOT/kubeconfig.json" [devshell] name = "kubenix" -packages = [ - "fd", - "dive", - "alejandra", - "kube3d", - "kubie", - "k9s", -] - -[[commands]] -name = "fmt" -help = "Check Nix formatting" -category = "checks" -command = "alejandra ${@} ." +packages = ["fd", "dive", "kube3d", "kubie", "k9s"] [[commands]] name = "evalnix" @@ -41,4 +28,3 @@ category = "k8s" [[commands]] package = "k9s" category = "k8s" - diff --git a/flake.lock b/flake.lock index 6849bbc..fd2ffaa 100644 --- a/flake.lock +++ b/flake.lock @@ -3,7 +3,9 @@ "devshell": { "inputs": { "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { "lastModified": 1647857022, @@ -19,6 +21,22 @@ "type": "github" } }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1648199409, + "narHash": "sha256-JwPKdC2PoVBkG6E+eWw3j6BMR6sL3COpYWfif7RVb8Y=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "64a525ee38886ab9028e6f61790de0832aa3ef03", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-utils": { "locked": { "lastModified": 1642700792, @@ -50,22 +68,6 @@ } }, "nixpkgs": { - "locked": { - "lastModified": 1643381941, - "narHash": "sha256-pHTwvnN4tTsEKkWlXQ8JMY423epos8wUOhthpwJjtpc=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "5efc8ca954272c4376ac929f4c5ffefcc20551d5", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { "locked": { "lastModified": 1648219316, "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=", @@ -84,8 +86,9 @@ "root": { "inputs": { "devshell": "devshell", + "flake-compat": "flake-compat", "flake-utils": "flake-utils_2", - "nixpkgs": "nixpkgs_2" + "nixpkgs": "nixpkgs" } } }, diff --git a/flake.nix b/flake.nix index 42fd3e9..4288cd0 100644 --- a/flake.nix +++ b/flake.nix @@ -2,9 +2,16 @@ description = "Kubernetes resource builder using nix"; inputs = { + flake-compat.url = "github:edolstra/flake-compat"; + flake-compat.flake = false; + flake-utils.url = "github:numtide/flake-utils"; + flake-utils.inputs.nixpkgs.follows = "nixpgks"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + devshell.url = "github:numtide/devshell"; + devshell.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { diff --git a/pkgs/applications/networking/cluster/kubernetes/mk-docker-opts.sh b/pkgs/applications/networking/cluster/kubernetes/mk-docker-opts.sh index 4650d09..7f1f1da 100644 --- a/pkgs/applications/networking/cluster/kubernetes/mk-docker-opts.sh +++ b/pkgs/applications/networking/cluster/kubernetes/mk-docker-opts.sh @@ -20,7 +20,7 @@ set -e usage() { - echo "$0 [-f FLANNEL-ENV-FILE] [-d DOCKER-ENV-FILE] [-i] [-c] [-m] [-k COMBINED-KEY] + echo "$0 [-f FLANNEL-ENV-FILE] [-d DOCKER-ENV-FILE] [-i] [-c] [-m] [-k COMBINED-KEY] Generate Docker daemon options based on flannel env file OPTIONS: @@ -31,7 +31,7 @@ OPTIONS: -k Set the combined options key to this value (default DOCKER_OPTS=) -m Do not output --ip-masq (useful for older Docker version) " >/dev/stderr - exit 1 + exit 1 } flannel_env="/run/flannel/subnet.env" @@ -43,53 +43,53 @@ ipmasq=true val="" while getopts "f:d:icmk:" opt; do - case $opt in - f) - flannel_env=$OPTARG - ;; - d) - docker_env=$OPTARG - ;; - i) - indiv_opts=true - ;; - c) - combined_opts=true - ;; - m) - ipmasq=false - ;; - k) - combined_opts_key=$OPTARG - ;; - \?) - usage - ;; - esac + case $opt in + f) + flannel_env=$OPTARG + ;; + d) + docker_env=$OPTARG + ;; + i) + indiv_opts=true + ;; + c) + combined_opts=true + ;; + m) + ipmasq=false + ;; + k) + combined_opts_key=$OPTARG + ;; + \?) + usage + ;; + esac done -if [[ $indiv_opts = false ]] && [[ $combined_opts = false ]]; then - indiv_opts=true - combined_opts=true +if [[ $indiv_opts == false ]] && [[ $combined_opts == false ]]; then + indiv_opts=true + combined_opts=true fi -if [[ -f "${flannel_env}" ]]; then - source "${flannel_env}" +if [[ -f ${flannel_env} ]]; then + source "${flannel_env}" fi -if [[ -n "$FLANNEL_SUBNET" ]]; then - # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below - DOCKER_OPT_BIP="--bip=$FLANNEL_SUBNET" +if [[ -n $FLANNEL_SUBNET ]]; then + # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below + DOCKER_OPT_BIP="--bip=$FLANNEL_SUBNET" fi -if [[ -n "$FLANNEL_MTU" ]]; then - # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below - DOCKER_OPT_MTU="--mtu=$FLANNEL_MTU" +if [[ -n $FLANNEL_MTU ]]; then + # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below + DOCKER_OPT_MTU="--mtu=$FLANNEL_MTU" fi -if [[ "$FLANNEL_IPMASQ" = true ]] && [[ $ipmasq = true ]]; then - # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below - DOCKER_OPT_IPMASQ="--ip-masq=false" +if [[ $FLANNEL_IPMASQ == true ]] && [[ $ipmasq == true ]]; then + # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below + DOCKER_OPT_IPMASQ="--ip-masq=false" fi eval docker_opts="\$${combined_opts_key}" @@ -99,15 +99,15 @@ echo -n "" >"${docker_env}" # OPT_LOOP for opt in $(compgen -v DOCKER_OPT_); do - eval val=\$"${opt}" + eval val=\$"${opt}" - if [[ "$indiv_opts" = true ]]; then - echo "$opt=\"$val\"" >>"${docker_env}" - fi + if [[ $indiv_opts == true ]]; then + echo "$opt=\"$val\"" >>"${docker_env}" + fi - docker_opts+="$val " + docker_opts+="$val " done -if [[ "$combined_opts" = true ]]; then - echo "${combined_opts_key}=\"${docker_opts}\"" >>"${docker_env}" +if [[ $combined_opts == true ]]; then + echo "${combined_opts_key}=\"${docker_opts}\"" >>"${docker_env}" fi diff --git a/shell.nix b/shell.nix index ecf415d..7a126f5 100644 --- a/shell.nix +++ b/shell.nix @@ -1,8 +1,14 @@ -{system ? builtins.currentSystem}: ( - (import ./compat.nix).flake-compat { - src = ./.; - inherit system; - } + import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${ + lock.nodes.flake-compat.locked.rev + }.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) {src = ./.;} ) .shellNix diff --git a/tests/k8s/deployment.yaml b/tests/k8s/deployment.yaml index f7f95de..506a113 100644 --- a/tests/k8s/deployment.yaml +++ b/tests/k8s/deployment.yaml @@ -15,7 +15,7 @@ spec: app: nginx spec: containers: - - name: nginx - image: nginx:1.7.9 - ports: - - containerPort: 80 + - name: nginx + image: nginx:1.7.9 + ports: + - containerPort: 80 diff --git a/tests/k8s/pod.json b/tests/k8s/pod.json index e586bc9..47c5e25 100644 --- a/tests/k8s/pod.json +++ b/tests/k8s/pod.json @@ -5,9 +5,11 @@ "name": "test" }, "spec": { - "containers": [{ - "name": "test", - "image": "busybox" - }] + "containers": [ + { + "name": "test", + "image": "busybox" + } + ] } } diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..c5469bd --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,25 @@ +[formatter.nix] +command = "alejandra" +includes = ["*.nix"] +excludes = ["./modules/generated/*"] + +[formatter.prettier] +command = "prettier" +options = ["--plugin", "prettier-plugin-toml", "--write"] +includes = ["*.md", "*.yaml", "*.toml", "*.json"] +excludes = [] + +[formatter.black] +command = "black" +includes = ["*.py"] + +[formatter.shell] +command = "shfmt" +options = [ + "-i", + "2", # indent 2 + "-s", # simplify the code + "-w", # write back to the file + +] +includes = ["*.sh", "*.bash"]