Skip to content

Inline bash scripts in Nix #1563

Closed
Closed
@steve-chavez

Description

@steve-chavez

Problem

There are various cases where bash scripts are source from within Nix and then they have their variables replaced with substitute:

  • postgres/flake.nix

    Lines 498 to 510 in 61ff753

    pkgs.runCommand "start-postgres-client" { } ''
    mkdir -p $out/bin
    substitute ${./nix/tools/run-client.sh.in} $out/bin/start-postgres-client \
    --subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
    --subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
    --subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
    --subst-var-by 'PSQL17_BINDIR' '${basePackages.psql_17.bin}' \
    --subst-var-by 'PSQLORIOLEDB17_BINDIR' '${basePackages.psql_orioledb-17.bin}' \
    --subst-var-by 'MIGRATIONS_DIR' '${migrationsDir}' \
    --subst-var-by 'POSTGRESQL_SCHEMA_SQL' '${postgresqlSchemaSql}' \
    --subst-var-by 'PGBOUNCER_AUTH_SCHEMA_SQL' '${pgbouncerAuthSchemaSql}' \
    --subst-var-by 'STAT_EXTENSION_SQL' '${statExtensionSql}'
    chmod +x $out/bin/start-postgres-client

  • postgres/flake.nix

    Lines 523 to 528 in 61ff753

    substitute ${./nix/tools/migrate-tool.sh.in} $out/bin/migrate-postgres \
    --subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
    --subst-var-by 'PSQL_CONF_FILE' '${configFile}' \
    --subst-var-by 'PGSODIUM_GETKEY' '${getkeyScript}' \
    --subst-var-by 'PRIMING_SCRIPT' '${primingScript}' \
    --subst-var-by 'MIGRATION_DATA' '${migrationData}'

  • postgres/flake.nix

    Lines 535 to 537 in 61ff753

    substitute ${./nix/tools/run-replica.sh.in} $out/bin/start-postgres-replica \
    --subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
    --subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'

This hurts reusability of the scripts logic and adds verboseness to the scripts.

Solution

Inline the scripts in the Nix file to avoid substitutions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions