Initial commit
This commit is contained in:
		
							
								
								
									
										47
									
								
								configuration.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								configuration.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
{
 | 
			
		||||
  imports = [
 | 
			
		||||
    ./formatters
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  services.simple-app = {
 | 
			
		||||
    enable = true;
 | 
			
		||||
    settings = {
 | 
			
		||||
      server = {
 | 
			
		||||
        port = 8080;
 | 
			
		||||
        host = "0.0.0.0";
 | 
			
		||||
      };
 | 
			
		||||
      logging = {
 | 
			
		||||
        level = "info";
 | 
			
		||||
        file = "/var/log/simple-app.log";
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
  services.validated-app = {
 | 
			
		||||
    enable = true;
 | 
			
		||||
    settings = {
 | 
			
		||||
      server = {
 | 
			
		||||
        port = 8080;
 | 
			
		||||
        host = "0.0.0.0";
 | 
			
		||||
      };
 | 
			
		||||
      logging = {
 | 
			
		||||
        level = "info";
 | 
			
		||||
        file = "/var/log/simple-app.log";
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
  services.multi-config-app = {
 | 
			
		||||
    enable = true;
 | 
			
		||||
    serverSettings = {
 | 
			
		||||
      port = 8000;
 | 
			
		||||
      host = "0.0.0.0";
 | 
			
		||||
    };
 | 
			
		||||
    databaseSettings = {
 | 
			
		||||
      url = "postgres://localhost/myapp";
 | 
			
		||||
      maxConnections = 50;
 | 
			
		||||
    };
 | 
			
		||||
    loggingSettings = {
 | 
			
		||||
      level = "warn";
 | 
			
		||||
      format = "text";
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								formatters/basic-formatters.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								formatters/basic-formatters.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
{pkgs ? import <nixpkgs> {}}: let
 | 
			
		||||
  # Initialize formatters
 | 
			
		||||
  yamlFormat = pkgs.formats.yaml {};
 | 
			
		||||
  jsonFormat = pkgs.formats.json {};
 | 
			
		||||
  tomlFormat = pkgs.formats.toml {};
 | 
			
		||||
  iniFormat = pkgs.formats.ini {};
 | 
			
		||||
 | 
			
		||||
  # Example configuration data
 | 
			
		||||
  exampleConfig = {
 | 
			
		||||
    server = {
 | 
			
		||||
      host = "localhost";
 | 
			
		||||
      port = 8080;
 | 
			
		||||
    };
 | 
			
		||||
    database = {
 | 
			
		||||
      url = "postgres://user:pass@localhost/db";
 | 
			
		||||
      maxConnections = 100;
 | 
			
		||||
    };
 | 
			
		||||
    logging = {
 | 
			
		||||
      level = "info";
 | 
			
		||||
      file = "/var/log/app.log";
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  # Generate files with each formatter
 | 
			
		||||
  yamlFile = yamlFormat.generate "config.yaml" exampleConfig;
 | 
			
		||||
  jsonFile = jsonFormat.generate "config.json" exampleConfig;
 | 
			
		||||
  tomlFile = tomlFormat.generate "config.toml" exampleConfig;
 | 
			
		||||
  iniFile = iniFormat.generate "config.ini" exampleConfig;
 | 
			
		||||
in
 | 
			
		||||
  pkgs.runCommand "formatter-examples" {} ''
 | 
			
		||||
    mkdir -p $out
 | 
			
		||||
    cp ${yamlFile} $out/config.yaml
 | 
			
		||||
    cp ${jsonFile} $out/config.json
 | 
			
		||||
    cp ${tomlFile} $out/config.toml
 | 
			
		||||
    cp ${iniFile} $out/config.ini
 | 
			
		||||
 | 
			
		||||
    # Create a simple script to display the files
 | 
			
		||||
    mkdir -p $out/bin
 | 
			
		||||
    cat > $out/bin/show-configs <<EOF
 | 
			
		||||
    #!/bin/sh
 | 
			
		||||
    echo "=== YAML Configuration ==="
 | 
			
		||||
    cat $out/config.yaml
 | 
			
		||||
    echo ""
 | 
			
		||||
 | 
			
		||||
    echo "=== JSON Configuration ==="
 | 
			
		||||
    cat $out/config.json
 | 
			
		||||
    echo ""
 | 
			
		||||
 | 
			
		||||
    echo "=== TOML Configuration ==="
 | 
			
		||||
    cat $out/config.toml
 | 
			
		||||
    echo ""
 | 
			
		||||
 | 
			
		||||
    echo "=== INI Configuration ==="
 | 
			
		||||
    cat $out/config.ini
 | 
			
		||||
    EOF
 | 
			
		||||
 | 
			
		||||
    chmod +x $out/bin/show-configs
 | 
			
		||||
  ''
 | 
			
		||||
							
								
								
									
										7
									
								
								formatters/default.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								formatters/default.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
  imports = [
 | 
			
		||||
    ./yaml-module.nix
 | 
			
		||||
    ./validated-app.nix
 | 
			
		||||
    ./multiple-files.nix
 | 
			
		||||
  ];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										67
									
								
								formatters/multiple-files.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								formatters/multiple-files.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
{
 | 
			
		||||
  config,
 | 
			
		||||
  lib,
 | 
			
		||||
  pkgs,
 | 
			
		||||
  ...
 | 
			
		||||
}:
 | 
			
		||||
with lib; let
 | 
			
		||||
  cfg = config.services.multi-config-app;
 | 
			
		||||
 | 
			
		||||
  format = pkgs.formats.yaml {};
 | 
			
		||||
 | 
			
		||||
  # Generate multiple config files
 | 
			
		||||
  serverConfigFile = format.generate "server.yaml" cfg.serverSettings;
 | 
			
		||||
  databaseConfigFile = format.generate "database.yaml" cfg.databaseSettings;
 | 
			
		||||
  loggingConfigFile = format.generate "logging.yaml" cfg.loggingSettings;
 | 
			
		||||
 | 
			
		||||
  # Create a directory with all configs
 | 
			
		||||
  configDir = pkgs.runCommand "multi-config-app-configs" {} ''
 | 
			
		||||
    mkdir -p $out
 | 
			
		||||
    cp ${serverConfigFile} $out/server.yaml
 | 
			
		||||
    cp ${databaseConfigFile} $out/database.yaml
 | 
			
		||||
    cp ${loggingConfigFile} $out/logging.yaml
 | 
			
		||||
  '';
 | 
			
		||||
in {
 | 
			
		||||
  options.services.multi-config-app = {
 | 
			
		||||
    enable = mkEnableOption "Multi-Configuration Application";
 | 
			
		||||
 | 
			
		||||
    serverSettings = mkOption {
 | 
			
		||||
      description = "Server configuration";
 | 
			
		||||
      type = format.type;
 | 
			
		||||
      default = {
 | 
			
		||||
        port = 8080;
 | 
			
		||||
        host = "127.0.0.1";
 | 
			
		||||
        workers = 4;
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    databaseSettings = mkOption {
 | 
			
		||||
      description = "Database configuration";
 | 
			
		||||
      type = format.type;
 | 
			
		||||
      default = {
 | 
			
		||||
        url = "postgres://localhost/app";
 | 
			
		||||
        maxConnections = 20;
 | 
			
		||||
        timeout = 30;
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    loggingSettings = mkOption {
 | 
			
		||||
      description = "Logging configuration";
 | 
			
		||||
      type = format.type;
 | 
			
		||||
      default = {
 | 
			
		||||
        level = "info";
 | 
			
		||||
        format = "json";
 | 
			
		||||
        output = "stdout";
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  config = mkIf cfg.enable {
 | 
			
		||||
    # For demonstration purposes, copy the generated configs to /tmp
 | 
			
		||||
    system.activationScripts.multiConfigApp = ''
 | 
			
		||||
      mkdir -p /tmp/multi-config-app
 | 
			
		||||
      cp -r ${configDir}/* /tmp/multi-config-app/
 | 
			
		||||
      echo "Multi-config app configs generated at /tmp/multi-config-app/"
 | 
			
		||||
    '';
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										78
									
								
								formatters/validated-app.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								formatters/validated-app.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,78 @@
 | 
			
		||||
{
 | 
			
		||||
  config,
 | 
			
		||||
  lib,
 | 
			
		||||
  pkgs,
 | 
			
		||||
  ...
 | 
			
		||||
}:
 | 
			
		||||
with lib; let
 | 
			
		||||
  cfg = config.services.validated-app;
 | 
			
		||||
 | 
			
		||||
  format = pkgs.formats.yaml {};
 | 
			
		||||
 | 
			
		||||
  configFile = format.generate "validated-app.yaml" cfg.settings;
 | 
			
		||||
in {
 | 
			
		||||
  options.services.validated-app = {
 | 
			
		||||
    enable = mkEnableOption "Validated Application";
 | 
			
		||||
 | 
			
		||||
    settings = mkOption {
 | 
			
		||||
      description = "Configuration for Validated Application";
 | 
			
		||||
      type = types.submodule {
 | 
			
		||||
        freeformType = format.type;
 | 
			
		||||
        options = {
 | 
			
		||||
          server = mkOption {
 | 
			
		||||
            type = types.submodule {
 | 
			
		||||
              options = {
 | 
			
		||||
                port = mkOption {
 | 
			
		||||
                  type = types.port;
 | 
			
		||||
                  default = 8080;
 | 
			
		||||
                  description = "Port to listen on";
 | 
			
		||||
                };
 | 
			
		||||
                host = mkOption {
 | 
			
		||||
                  type = types.str;
 | 
			
		||||
                  default = "127.0.0.1";
 | 
			
		||||
                  description = "Host to bind to";
 | 
			
		||||
                };
 | 
			
		||||
                workers = mkOption {
 | 
			
		||||
                  type = types.ints.positive;
 | 
			
		||||
                  default = 4;
 | 
			
		||||
                  description = "Number of worker processes";
 | 
			
		||||
                };
 | 
			
		||||
              };
 | 
			
		||||
            };
 | 
			
		||||
            default = {};
 | 
			
		||||
            description = "Server configuration";
 | 
			
		||||
          };
 | 
			
		||||
 | 
			
		||||
          logging = mkOption {
 | 
			
		||||
            type = types.submodule {
 | 
			
		||||
              options = {
 | 
			
		||||
                level = mkOption {
 | 
			
		||||
                  type = types.enum ["debug" "info" "warn" "error"];
 | 
			
		||||
                  default = "info";
 | 
			
		||||
                  description = "Logging level";
 | 
			
		||||
                };
 | 
			
		||||
                file = mkOption {
 | 
			
		||||
                  type = types.nullOr types.str;
 | 
			
		||||
                  default = null;
 | 
			
		||||
                  description = "Log file path (null for stdout)";
 | 
			
		||||
                };
 | 
			
		||||
              };
 | 
			
		||||
            };
 | 
			
		||||
            default = {};
 | 
			
		||||
            description = "Logging configuration";
 | 
			
		||||
          };
 | 
			
		||||
        };
 | 
			
		||||
      };
 | 
			
		||||
      default = {};
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  config = mkIf cfg.enable {
 | 
			
		||||
    # For demonstration purposes, copy the generated config to /tmp
 | 
			
		||||
    system.activationScripts.validatedAppConfig = ''
 | 
			
		||||
      mkdir -p /tmp/validated-app
 | 
			
		||||
      cp ${configFile} /tmp/validated-app/config.yaml
 | 
			
		||||
      echo "Validated app config generated at /tmp/validated-app/config.yaml"
 | 
			
		||||
    '';
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								formatters/yaml-module.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								formatters/yaml-module.nix
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
{
 | 
			
		||||
  config,
 | 
			
		||||
  lib,
 | 
			
		||||
  pkgs,
 | 
			
		||||
  ...
 | 
			
		||||
}:
 | 
			
		||||
with lib; let
 | 
			
		||||
  cfg = config.services.simple-app;
 | 
			
		||||
 | 
			
		||||
  format = pkgs.formats.yaml {};
 | 
			
		||||
 | 
			
		||||
  configFile = format.generate "simple-app.yaml" cfg.settings;
 | 
			
		||||
in {
 | 
			
		||||
  options.services.simple-app = {
 | 
			
		||||
    enable = mkEnableOption "Simple Application";
 | 
			
		||||
 | 
			
		||||
    settings = mkOption {
 | 
			
		||||
      description = "Configuration for Simple Application";
 | 
			
		||||
      type = format.type;
 | 
			
		||||
      default = {};
 | 
			
		||||
      example = {
 | 
			
		||||
        server = {
 | 
			
		||||
          port = 8080;
 | 
			
		||||
          host = "0.0.0.0";
 | 
			
		||||
        };
 | 
			
		||||
        logging = {
 | 
			
		||||
          level = "info";
 | 
			
		||||
        };
 | 
			
		||||
      };
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  config = mkIf cfg.enable {
 | 
			
		||||
    # For demonstration purposes, copy the generated config to /tmp
 | 
			
		||||
    system.activationScripts.simpleAppConfig = ''
 | 
			
		||||
      mkdir -p /tmp/simple-app
 | 
			
		||||
      cp ${configFile} /tmp/simple-app/config.yaml
 | 
			
		||||
      echo "Simple app config generated at /tmp/simple-app/config.yaml"
 | 
			
		||||
    '';
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user