Skip to content

Commit f69052f

Browse files
committed
chore: migrate scripts generated by runCommand to writeShellApplication
`writeShellApplication` avoids substitute commands, provides the correct package metadata and make sure that all scripts are shellcheked.
1 parent 0c2c43c commit f69052f

13 files changed

+713
-830
lines changed

nix/apps.nix

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
{ ... }:
22
{
33
perSystem =
4-
{ self', ... }:
4+
{ self', lib, ... }:
55
let
6-
mkApp = attrName: binName: {
6+
mkApp = attrName: {
77
type = "app";
8-
program = "${self'.packages."${attrName}"}/bin/${binName}";
8+
program = lib.getExe self'.packages."${attrName}";
99
};
1010
in
1111
{
1212
# Apps is a list of names of things that can be executed with 'nix run';
1313
# these are distinct from the things that can be built with 'nix build',
1414
# so they need to be listed here too.
1515
apps = {
16-
start-server = mkApp "start-server" "start-postgres-server";
17-
start-client = mkApp "start-client" "start-postgres-client";
18-
start-replica = mkApp "start-replica" "start-postgres-replica";
19-
# migrate-postgres = mkApp "migrate-tool" "migrate-postgres";
20-
# sync-exts-versions = mkApp "sync-exts-versions" "sync-exts-versions";
21-
pg-restore = mkApp "pg-restore" "pg-restore";
22-
local-infra-bootstrap = mkApp "local-infra-bootstrap" "local-infra-bootstrap";
23-
dbmate-tool = mkApp "dbmate-tool" "dbmate-tool";
24-
update-readme = mkApp "update-readme" "update-readme";
25-
show-commands = mkApp "show-commands" "show-commands";
26-
build-test-ami = mkApp "build-test-ami" "build-test-ami";
27-
run-testinfra = mkApp "run-testinfra" "run-testinfra";
28-
cleanup-ami = mkApp "cleanup-ami" "cleanup-ami";
29-
trigger-nix-build = mkApp "trigger-nix-build" "trigger-nix-build";
16+
start-server = mkApp "start-server";
17+
start-client = mkApp "start-client";
18+
start-replica = mkApp "start-replica";
19+
# migrate-postgres = mkApp "migrate-tool";
20+
# sync-exts-versions = mkApp "sync-exts-versions";
21+
pg-restore = mkApp "pg-restore";
22+
local-infra-bootstrap = mkApp "local-infra-bootstrap";
23+
dbmate-tool = mkApp "dbmate-tool";
24+
update-readme = mkApp "update-readme";
25+
show-commands = mkApp "show-commands";
26+
build-test-ami = mkApp "build-test-ami";
27+
run-testinfra = mkApp "run-testinfra";
28+
cleanup-ami = mkApp "cleanup-ami";
29+
trigger-nix-build = mkApp "trigger-nix-build";
3030
};
3131
};
3232
}

nix/packages/build-test-ami.nix

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
{ pkgs, runCommand }:
2-
runCommand "build-test-ami"
3-
{
4-
buildInputs = with pkgs; [
5-
packer
6-
awscli2
7-
yq
8-
jq
9-
openssl
10-
git
11-
coreutils
12-
aws-vault
13-
];
14-
}
15-
''
16-
mkdir -p $out/bin
17-
cat > $out/bin/build-test-ami << 'EOL'
18-
#!/usr/bin/env bash
19-
set -euo pipefail
20-
1+
{
2+
writeShellApplication,
3+
packer,
4+
awscli2,
5+
yq,
6+
jq,
7+
openssl,
8+
gitMinimal,
9+
coreutils,
10+
aws-vault,
11+
python3,
12+
}:
13+
writeShellApplication {
14+
name = "build-test-ami";
15+
runtimeInputs = [
16+
packer
17+
awscli2
18+
yq
19+
jq
20+
openssl
21+
gitMinimal
22+
coreutils
23+
aws-vault
24+
python3
25+
];
26+
text = ''
2127
show_help() {
2228
cat << EOF
2329
Usage: build-test-ami [--help] <postgres-version>
@@ -52,30 +58,6 @@ runCommand "build-test-ami"
5258
exit 0
5359
fi
5460
55-
export PATH="${
56-
pkgs.lib.makeBinPath (
57-
with pkgs;
58-
[
59-
packer
60-
awscli2
61-
yq
62-
jq
63-
openssl
64-
git
65-
coreutils
66-
aws-vault
67-
]
68-
)
69-
}:$PATH"
70-
71-
# Check for required tools
72-
for cmd in packer aws-vault yq jq openssl; do
73-
if ! command -v $cmd &> /dev/null; then
74-
echo "Error: $cmd is required but not found"
75-
exit 1
76-
fi
77-
done
78-
7961
# Check AWS Vault profile
8062
if [ -z "''${AWS_VAULT:-}" ]; then
8163
echo "Error: AWS_VAULT environment variable must be set with the profile name"
@@ -140,18 +122,18 @@ runCommand "build-test-ami"
140122
VENV_DIR=$(mktemp -d)
141123
trap 'rm -rf "$VENV_DIR"' EXIT HUP INT QUIT TERM
142124
python3 -m venv "$VENV_DIR"
125+
# shellcheck source=/dev/null
143126
source "$VENV_DIR/bin/activate"
144127
145128
# Install required Python packages
146129
echo "Installing required Python packages..."
147-
pip install boto3 boto3-stubs[essential] docker ec2instanceconnectcli pytest paramiko requests
130+
pip install boto3 'boto3-stubs[essential]' docker ec2instanceconnectcli pytest paramiko requests
148131
149132
# Run the tests with aws-vault
150133
echo "Running tests for AMI: $RANDOM_STRING using AWS Vault profile: $AWS_VAULT_PROFILE"
151-
aws-vault exec $AWS_VAULT_PROFILE -- pytest -vv -s testinfra/test_ami_nix.py
134+
aws-vault exec "$AWS_VAULT_PROFILE" -- pytest -vv -s testinfra/test_ami_nix.py
152135
153136
# Deactivate virtual environment (cleanup is handled by trap)
154137
deactivate
155-
EOL
156-
chmod +x $out/bin/build-test-ami
157-
''
138+
'';
139+
}

nix/packages/cleanup-ami.nix

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
1-
{ pkgs, runCommand }:
2-
runCommand "cleanup-ami"
3-
{
4-
buildInputs = with pkgs; [
5-
awscli2
6-
aws-vault
7-
];
8-
}
9-
''
10-
mkdir -p $out/bin
11-
cat > $out/bin/cleanup-ami << 'EOL'
12-
#!/usr/bin/env bash
13-
set -euo pipefail
14-
15-
export PATH="${
16-
pkgs.lib.makeBinPath (
17-
with pkgs;
18-
[
19-
awscli2
20-
aws-vault
21-
]
22-
)
23-
}:$PATH"
24-
25-
# Check for required tools
26-
for cmd in aws-vault; do
27-
if ! command -v $cmd &> /dev/null; then
28-
echo "Error: $cmd is required but not found"
29-
exit 1
30-
fi
31-
done
32-
1+
{
2+
writeShellApplication,
3+
awscli2,
4+
aws-vault,
5+
}:
6+
writeShellApplication {
7+
name = "cleanup-ami";
8+
runtimeInputs = [
9+
awscli2
10+
aws-vault
11+
];
12+
text = ''
3313
# Check AWS Vault profile
3414
if [ -z "''${AWS_VAULT:-}" ]; then
3515
echo "Error: AWS_VAULT environment variable must be set with the profile name"
@@ -56,6 +36,5 @@ runCommand "cleanup-ami"
5636
aws ec2 deregister-image --region $REGION --image-id "$ami_id" || true
5737
done
5838
done
59-
EOL
60-
chmod +x $out/bin/cleanup-ami
61-
''
39+
'';
40+
}

0 commit comments

Comments
 (0)