diff --git a/README.md b/README.md index 7b1dc00..03ad03d 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,9 @@ The flake schemas are also helpful: ```text { - inherit (inputs.up) schemas; + schemas = inputs.up.exportedSchemas // { + # other schemas + }; } ``` @@ -154,3 +156,32 @@ To run it: ```shell nix run ".#taskRunners..default" ``` + +## Environment variable sets + +There are two types of environment variable sets: **static** and **computed**. +Static sets are attribute sets of strings: + +```nix +{ + staticEnvVars.postgres = { + PGDATA = ".state/postgres"; + PGDATABASE = "testing"; + PGHOST = "127.0.0.1"; + PGPORT = toString 5432; + }; +} +``` + +Computed sets are system specific and may be based on things like packages in Nixpkgs: + +```nix +computedEnvVars = forEachSupportedSystem ( + { pkgs, system }: + { + postgres = { + PGSSLCERT = "${pkgs.postgresql}/share/postgresql/root.crt"; + }; + } +); +``` diff --git a/flake.nix b/flake.nix index 1cdadcc..5161937 100644 --- a/flake.nix +++ b/flake.nix @@ -46,6 +46,8 @@ shellHook = '' ${self.taskRunners.${system}.default.shellHook} ''; + + env = self.computedEnvVars.${system}.openssl; }; } ); @@ -54,23 +56,37 @@ lib = import ./nix/lib.nix { inherit lib; }; + staticEnvVars.postgres = { + PGDATA = ".state/postgres"; + PGDATABASE = "testing"; + PGHOST = "127.0.0.1"; + PGPORT = "5432"; + }; + + computedEnvVars = forEachSupportedSystem ( + { pkgs, system }: + { + openssl = { + OPENSSL_DIR = "${pkgs.openssl.dev}"; + OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include"; + }; + } + ); + processTrees = forEachSupportedSystem ( { pkgs, system }: { - data = pkgs.lib.mkProcessTree { + postgres = pkgs.lib.mkProcessTree { description = "Run Postgres locally"; packages = with pkgs; [ (postgresql_18.withPackages (p: with p; [ pg_uuidv7 ])) + openssl redis ]; - environment = { - PGDATA = ".state/postgres"; - PGDATABASE = "testing"; - PGHOST = "127.0.0.1"; - PGPORT = "5432"; - }; + environment = self.staticEnvVars.postgres // self.computedEnvVars.${system}.openssl; processes = { postgres-setup = { @@ -174,7 +190,32 @@ schemas ; } - // { + // self.exportedSchemas; + + exportedSchemas = { + exportedSchemas = { + version = 1; + doc = '' + The `exportedSchemas` flake output is used to define flake schemas that you + intend for other flakes to use. + ''; + + inventory = + output: + inputs.flake-schemas.lib.mkChildren ( + builtins.mapAttrs (schemaName: schemaDef: { + shortDescription = "A schema checker for the `${schemaName}` flake output"; + evalChecks.isValidSchema = + schemaDef.version or 0 == 1 + && schemaDef ? doc + && builtins.isString (schemaDef.doc) + && schemaDef ? inventory + && builtins.isFunction (schemaDef.inventory); + what = "flake schema"; + }) output + ); + }; + taskRunners = { version = 1; doc = '' @@ -196,35 +237,49 @@ ); }; - envVars = { + staticEnvVars = { version = 1; doc = '' - The `envVars` output provides sets of environment variables + The `staticEnvVars` output provides sets of environment variables that can be sourced into shells or consumed by other tools. ''; inventory = output: let isEnv = v: builtins.isAttrs v && builtins.all (s: builtins.isString s) (builtins.attrValues v); - isPerSystem = builtins.all (v: builtins.isAttrs v && !isEnv v) (builtins.attrValues output); in inputs.flake-schemas.lib.mkChildren ( - if isPerSystem then - builtins.mapAttrs (system: envs: { + builtins.mapAttrs (_name: env: { + evalChecks.isAttrs = builtins.isAttrs env; + evalChecks.allStrings = isEnv env; + what = "environment variables set"; + }) output + ); + }; + + computedEnvVars = { + version = 1; + doc = '' + The `computedEnvVars` output provides sets of environment variables + that can be sourced into shells or consumed by other tools. Unlike `staticEnvVars`, these + sets are system specific and involve some kind of computation (like using packages from Nixpkgs). + ''; + appendSystem = true; + inventory = + output: + let + isEnv = v: builtins.isAttrs v && builtins.all (s: builtins.isString s) (builtins.attrValues v); + in + inputs.flake-schemas.lib.mkChildren ( + builtins.mapAttrs (system: envs: { + forSystems = [ system ]; + children = builtins.mapAttrs (_name: env: { forSystems = [ system ]; - children = builtins.mapAttrs (_name: env: { - forSystems = [ system ]; - evalChecks.isAttrs = builtins.isAttrs env; - evalChecks.allStrings = isEnv env; - what = "environment variable set"; - }) envs; - }) output - else - builtins.mapAttrs (_name: env: { evalChecks.isAttrs = builtins.isAttrs env; evalChecks.allStrings = isEnv env; - what = "environment variables set"; - }) output + what = "computed environment variable set"; + }) envs; + }) output ); };