add treefmt

This commit is contained in:
GTrunSec 2022-04-02 15:40:44 -07:00
parent 91d1041e06
commit f5d82afca7
No known key found for this signature in database
GPG key ID: 2368FAFA4ABDD2A0
11 changed files with 147 additions and 103 deletions

View file

@ -1,6 +0,0 @@
{
flake-compat = import (builtins.fetchurl {
url = "https://raw.githubusercontent.com/edolstra/flake-compat/99f1c2157fba4bfe6211a321fd0ee43199025dbf/default.nix";
sha256 = "1vas5z58901gavy5d53n1ima482yvly405jp9l8g07nr4abmzsyb";
});
}

View file

@ -1,8 +1,14 @@
{system ? builtins.currentSystem}:
( (
(import ./compat.nix).flake-compat { import (
src = ./.; let
inherit system; 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 .defaultNix

View file

@ -4,4 +4,19 @@
}: }:
pkgs.devshell.mkShell { pkgs.devshell.mkShell {
imports = [(pkgs.devshell.importTOML ./devshell.toml)]; 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
'';
} }

View file

@ -8,20 +8,7 @@ eval = "$DEVSHELL_ROOT/kubeconfig.json"
[devshell] [devshell]
name = "kubenix" name = "kubenix"
packages = [ packages = ["fd", "dive", "kube3d", "kubie", "k9s"]
"fd",
"dive",
"alejandra",
"kube3d",
"kubie",
"k9s",
]
[[commands]]
name = "fmt"
help = "Check Nix formatting"
category = "checks"
command = "alejandra ${@} ."
[[commands]] [[commands]]
name = "evalnix" name = "evalnix"
@ -41,4 +28,3 @@ category = "k8s"
[[commands]] [[commands]]
package = "k9s" package = "k9s"
category = "k8s" category = "k8s"

39
flake.lock generated
View file

@ -3,7 +3,9 @@
"devshell": { "devshell": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"nixpkgs": "nixpkgs" "nixpkgs": [
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1647857022, "lastModified": 1647857022,
@ -19,6 +21,22 @@
"type": "github" "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": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1642700792, "lastModified": 1642700792,
@ -50,22 +68,6 @@
} }
}, },
"nixpkgs": { "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": { "locked": {
"lastModified": 1648219316, "lastModified": 1648219316,
"narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=", "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=",
@ -84,8 +86,9 @@
"root": { "root": {
"inputs": { "inputs": {
"devshell": "devshell", "devshell": "devshell",
"flake-compat": "flake-compat",
"flake-utils": "flake-utils_2", "flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs"
} }
} }
}, },

View file

@ -2,9 +2,16 @@
description = "Kubernetes resource builder using nix"; description = "Kubernetes resource builder using nix";
inputs = { inputs = {
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
flake-utils.inputs.nixpkgs.follows = "nixpgks";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
devshell.url = "github:numtide/devshell"; devshell.url = "github:numtide/devshell";
devshell.inputs.nixpkgs.follows = "nixpkgs";
}; };
outputs = { outputs = {

View file

@ -20,7 +20,7 @@
set -e set -e
usage() { 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 Generate Docker daemon options based on flannel env file
OPTIONS: OPTIONS:
@ -31,7 +31,7 @@ OPTIONS:
-k Set the combined options key to this value (default DOCKER_OPTS=) -k Set the combined options key to this value (default DOCKER_OPTS=)
-m Do not output --ip-masq (useful for older Docker version) -m Do not output --ip-masq (useful for older Docker version)
" >/dev/stderr " >/dev/stderr
exit 1 exit 1
} }
flannel_env="/run/flannel/subnet.env" flannel_env="/run/flannel/subnet.env"
@ -43,53 +43,53 @@ ipmasq=true
val="" val=""
while getopts "f:d:icmk:" opt; do while getopts "f:d:icmk:" opt; do
case $opt in case $opt in
f) f)
flannel_env=$OPTARG flannel_env=$OPTARG
;; ;;
d) d)
docker_env=$OPTARG docker_env=$OPTARG
;; ;;
i) i)
indiv_opts=true indiv_opts=true
;; ;;
c) c)
combined_opts=true combined_opts=true
;; ;;
m) m)
ipmasq=false ipmasq=false
;; ;;
k) k)
combined_opts_key=$OPTARG combined_opts_key=$OPTARG
;; ;;
\?) \?)
usage usage
;; ;;
esac esac
done done
if [[ $indiv_opts = false ]] && [[ $combined_opts = false ]]; then if [[ $indiv_opts == false ]] && [[ $combined_opts == false ]]; then
indiv_opts=true indiv_opts=true
combined_opts=true combined_opts=true
fi fi
if [[ -f "${flannel_env}" ]]; then if [[ -f ${flannel_env} ]]; then
source "${flannel_env}" source "${flannel_env}"
fi fi
if [[ -n "$FLANNEL_SUBNET" ]]; then if [[ -n $FLANNEL_SUBNET ]]; then
# shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below
DOCKER_OPT_BIP="--bip=$FLANNEL_SUBNET" DOCKER_OPT_BIP="--bip=$FLANNEL_SUBNET"
fi fi
if [[ -n "$FLANNEL_MTU" ]]; then if [[ -n $FLANNEL_MTU ]]; then
# shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below
DOCKER_OPT_MTU="--mtu=$FLANNEL_MTU" DOCKER_OPT_MTU="--mtu=$FLANNEL_MTU"
fi fi
if [[ "$FLANNEL_IPMASQ" = true ]] && [[ $ipmasq = true ]]; then if [[ $FLANNEL_IPMASQ == true ]] && [[ $ipmasq == true ]]; then
# shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below # shellcheck disable=SC2034 # Variable name referenced in OPT_LOOP below
DOCKER_OPT_IPMASQ="--ip-masq=false" DOCKER_OPT_IPMASQ="--ip-masq=false"
fi fi
eval docker_opts="\$${combined_opts_key}" eval docker_opts="\$${combined_opts_key}"
@ -99,15 +99,15 @@ echo -n "" >"${docker_env}"
# OPT_LOOP # OPT_LOOP
for opt in $(compgen -v DOCKER_OPT_); do for opt in $(compgen -v DOCKER_OPT_); do
eval val=\$"${opt}" eval val=\$"${opt}"
if [[ "$indiv_opts" = true ]]; then if [[ $indiv_opts == true ]]; then
echo "$opt=\"$val\"" >>"${docker_env}" echo "$opt=\"$val\"" >>"${docker_env}"
fi fi
docker_opts+="$val " docker_opts+="$val "
done done
if [[ "$combined_opts" = true ]]; then if [[ $combined_opts == true ]]; then
echo "${combined_opts_key}=\"${docker_opts}\"" >>"${docker_env}" echo "${combined_opts_key}=\"${docker_opts}\"" >>"${docker_env}"
fi fi

View file

@ -1,8 +1,14 @@
{system ? builtins.currentSystem}:
( (
(import ./compat.nix).flake-compat { import (
src = ./.; let
inherit system; 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 .shellNix

View file

@ -15,7 +15,7 @@ spec:
app: nginx app: nginx
spec: spec:
containers: containers:
- name: nginx - name: nginx
image: nginx:1.7.9 image: nginx:1.7.9
ports: ports:
- containerPort: 80 - containerPort: 80

View file

@ -5,9 +5,11 @@
"name": "test" "name": "test"
}, },
"spec": { "spec": {
"containers": [{ "containers": [
"name": "test", {
"image": "busybox" "name": "test",
}] "image": "busybox"
}
]
} }
} }

25
treefmt.toml Normal file
View file

@ -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"]