Skip to content

Commit

Permalink
Adopt flake-parts
Browse files Browse the repository at this point in the history
  • Loading branch information
linyinfeng committed Mar 11, 2023
1 parent f69188f commit e14f863
Show file tree
Hide file tree
Showing 304 changed files with 4,374 additions and 4,466 deletions.
43 changes: 0 additions & 43 deletions .editorconfig

This file was deleted.

2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
nix_direnv_watch_file $(find shell)
nix_direnv_watch_file $(find devshell) flake/nixpkgs.nix
use flake
43 changes: 43 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Check"
on:
push:
pull_request:
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@main
with:
token: '${{ secrets.PAT_FOR_AUTOMATED_UPDATE }}'
- name: Install nix
uses: cachix/install-nix-action@master
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Setup cachix
uses: cachix/cachix-action@master
with:
name: linyinfeng
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- name: Set swap space
uses: pierotofy/set-swap-space@master
with:
swap-size-gb: 10
- name: Test nix flake show
run: |
nix flake show
- name: Test evaluation
run: |
set -e
system=$(nix eval --impure --raw --expr "builtins.currentSystem")
checks=$(nix eval --impure --json --expr "builtins.attrNames (builtins.getFlake \"$PWD\").checks.$system")
for check in $(echo "$checks" | jq -r '.[]'); do
echo "--- eval .#checks.$system.$check"
nix eval --raw .#checks.$system.$check --accept-flake-config
echo # add a new line
done
5 changes: 2 additions & 3 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ jobs:
- name: Nix flake update
run: |
nix flake update --commit-lock-file
- name: Update pkgs
- name: Test nix flake show
run: |
nix develop --command nvfetcher
nix develop --command nvfetcher-changes-commit "pkgs/_sources/generated.nix"
nix flake show
- name: Test evaluation
run: |
set -e
Expand Down
20 changes: 4 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
MIT License

Copyright (c) 2021 Lin Yinfeng
Copyright (c) 2021-2023 Lin Yinfeng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

[![Hydra Build](https://img.shields.io/endpoint?label=main%2Fall-checks.x86_64-linux&url=https%3A%2F%2Fnuc-proxy.li7g.com%2Fhydra%2Fjob%2Fdotfiles%2Fmain%2Fall-checks.x86_64-linux%2Fshield)](https://nuc-proxy.li7g.com/hydra/job/dotfiles/main/all-checks.x86_64-linux)

My personal NixOS configurations, based on [devos].
Personal NixOS, Home Manager and Terraform configurations.

## License

This repository and [devos] are both licensed under [The MIT License][mit].
This repository is licensed under [The MIT License][mit].

[devos]: https://github.com/divnix/devos "divnix/devos"
[mit]: https://opensource.org/licenses/MIT "The MIT License"
27 changes: 27 additions & 0 deletions devshell/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{...}: {
imports = [
./terraform
];
perSystem = {
inputs',
pkgs,
...
}: {
devshells.default = {
commands = [
{
package = inputs'.nixos-generators.packages.nixos-generate;
category = "deploy";
}
{
package = pkgs.sops;
category = "secrets";
}
{
package = pkgs.ssh-to-age;
category = "secrets";
}
];
};
};
}
118 changes: 118 additions & 0 deletions devshell/terraform/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{...}: {
perSystem = {pkgs, ...}: let
secretExtractor = pkgs.callPackage ./extractor/secrets.nix {};
dataExtractor = pkgs.callPackage ./extractor/data.nix {};
in {
devshells.default.commands = [
{
category = "infrastructure";
name = "terraform-pipe";
help = "initialize, apply, and update all terraform related output files";
command = ''
set -e
terraform-init
terraform-wrapper apply
terraform-update-outputs
terraform-outputs-extract-data
terraform-outputs-extract-secrets
'';
}
{
category = "infrastructure";
name = "terraform-wrapper";
help = pkgs.terraform.meta.description;
command = ''
set -e
export PATH=${pkgs.sops}/bin:$PATH
export PATH=${pkgs.terraform}/bin:$PATH
export PATH=${pkgs.zerotierone}/bin:$PATH
export PATH=${pkgs.minio-client}/bin:$PATH
export PATH=${pkgs.syncthing}/bin:$PATH
export PATH=${pkgs.jq}/bin:$PATH
export PATH=${pkgs.openssl}/bin:$PATH
export PATH=${pkgs.ruby}/bin:$PATH
encrypted_state_file="$PRJ_ROOT/secrets/terraform.tfstate"
unencrypted_state_file="$PRJ_ROOT/terraform/terraform.tfstate"
echo "decrypt terraform state to '$unencrypted_state_file'..." >&2
sops --input-type json --output-type json \
--decrypt "$encrypted_state_file" > "$unencrypted_state_file"
function cleanup {
cd $PRJ_ROOT
if [ -n "$(cat "$unencrypted_state_file")" ]; then
echo "encrypt terraform state to '$encrypted_state_file'..." >&2
set +e
EDITOR="cp \"$unencrypted_state_file\"" \
sops --input-type json --output-type json \
"$encrypted_state_file"
encrypt_status="$?"
set -e
rm "$unencrypted_state_file"
if [ "$encrypt_status" -ne 0 -a "$encrypt_status" -ne 200 ]; then
echo "failed to encrypt, exiting"
exit 1
fi
fi
}
trap cleanup EXIT
cd $PRJ_ROOT/terraform
terraform "$@"
'';
}

{
category = "infrastructure";
name = "terraform-update-outputs";
help = "update terraform outputs";
command = ''
set -e
export PATH=${pkgs.sops}/bin:$PATH
encrypted_output_file="$PRJ_ROOT/secrets/terraform-outputs.yaml"
unencrypted_output_file="$PRJ_ROOT/secrets/terraform-outputs.plain.yaml"
terraform-wrapper output --json > "$unencrypted_output_file"
set +e
EDITOR="cp \"$unencrypted_output_file\"" sops "$encrypted_output_file"
encrypt_status="$?"
set -e
rm "$unencrypted_output_file"
if [ "$encrypt_status" -ne 0 -a "$encrypt_status" -ne 200 ]; then
echo "failed to encrypt, exiting"
exit 1
fi
'';
}

{
category = "infrastructure";
name = "terraform-outputs-extract-secrets";
help = "extract secrets from terraform outputs";
package = secretExtractor;
}

{
category = "infrastructure";
name = "terraform-outputs-extract-data";
help = "extract data from terraform outputs";
package = dataExtractor;
}

{
category = "infrastructure";
name = "terraform-init";
help = "upgrade terraform providers";
command = ''
set -e
cd $PRJ_ROOT/terraform
${pkgs.terraform}/bin/terraform init "$@"
'';
}
];
};
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{ writeShellScriptBin, yq-go, sops }:

{
writeShellScriptBin,
yq-go,
sops,
}:
writeShellScriptBin "terraform-outputs-extract-data" ''
set -e
export PATH=${yq-go}/bin:${sops}/bin:$PATH
pushd $PRJ_ROOT/data
pushd $PRJ_ROOT/lib/data
echo "creating 'data/data.json'..."
echo "creating 'data.json'..."
sops exec-file $PRJ_ROOT/secrets/terraform-outputs.yaml \
"yq eval --from-file \"template.yq\" {} --output-format json" \
> "data.json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{ writeShellScriptBin, yq-go, sops, fd }:

{
writeShellScriptBin,
yq-go,
sops,
fd,
}:
writeShellScriptBin "terraform-outputs-extract-secrets" ''
set -e
Expand Down
2 changes: 2 additions & 0 deletions flake-modules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading

0 comments on commit e14f863

Please sign in to comment.