{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schemas.idkyet.fml128.ch/v1/layer.schema.json",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "type": "integer",
      "const": 1,
      "description": "Layer config schema version."
    },
    "id": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*/[A-Za-z0-9._/-]+$",
      "description": "OCI image id for this layer. Case-insensitive; normalized to lowercase by the builder."
    },
    "previous_layer": {
      "type": "string",
      "minLength": 1,
      "description": "scratch, an internal layer id, or an external OCI image reference with tag/digest. Case-insensitive for internal ids."
    },
    "metadata": {
      "$ref": "#/definitions/metadata"
    },
    "requirements": {
      "$ref": "#/definitions/requirements"
    },
    "build": {
      "$ref": "#/definitions/build"
    },
    "arch": {
      "type": "string",
      "enum": ["x86_64_v3"],
      "default": "x86_64_v3",
      "description": "Target CPU arch/microarch used to select the pacman.conf shipped in scratch layers."
    },
    "source_path": {
      "type": "string",
      "minLength": 1,
      "description": "Optional host-side directory whose contents are copied into the merged overlay. Defaults to <base>/<layer-name>/files."
    },
    "packages": {
      "$ref": "#/definitions/packages"
    },
    "tasks": {
      "$ref": "#/definitions/tasks"
    },
    "services": {
      "$ref": "#/definitions/services"
    },
    "boot": {
      "$ref": "#/definitions/boot"
    },
    "installer": {
      "$ref": "#/definitions/installer"
    },
    "security": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "aur_scan": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "ignore": {
              "type": "object",
              "additionalProperties": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              }
            }
          }
        }
      }
    }
  },
  "required": ["schema_version", "id", "previous_layer", "build"],
  "definitions": {
    "string_list": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "metadata": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "display_name": { "type": "string", "minLength": 1 },
        "description": { "type": "string", "minLength": 1 },
        "vendor": { "type": "string", "minLength": 1 },
        "documentation_url": { "type": "string", "format": "uri" },
        "icon": { "type": "string", "minLength": 1 }
      }
    },
    "requirements": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "min_ram_mib": { "type": "integer", "minimum": 1 },
        "min_disk_gib": { "type": "integer", "minimum": 1 },
        "requires_efi": { "type": "boolean" },
        "requires_secure_boot": { "type": "boolean" },
        "requires_tpm2": { "type": "boolean" },
        "min_cpu_features": { "$ref": "#/definitions/string_list" }
      }
    },
    "build": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "push": {
          "type": "boolean",
          "default": false
        },
        "artifacts": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["image", "efi", "iso"]
          },
          "minItems": 1,
          "uniqueItems": true
        }
      },
      "required": ["artifacts"]
    },
    "packages": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "official": { "$ref": "#/definitions/string_list" },
        "aur": { "$ref": "#/definitions/string_list" }
      }
    },
    "tasks": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "pre": { "$ref": "#/definitions/string_list" },
        "post": { "$ref": "#/definitions/string_list" }
      }
    },
    "services": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "enable": { "$ref": "#/definitions/string_list" },
        "disable": { "$ref": "#/definitions/string_list" },
        "mask": { "$ref": "#/definitions/string_list" }
      }
    },
    "boot": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "secure_boot": {
          "type": "boolean",
          "default": false
        },
        "kernel_parameters": { "$ref": "#/definitions/string_list" }
      }
    },
    "installer": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "schema_version": { "type": "integer", "const": 1 },
        "target_selection": { "$ref": "#/definitions/target_selection" },
        "partition_table": { "type": "string", "enum": ["gpt", "mbr"], "default": "gpt" },
        "partitions": {
          "type": "array",
          "items": { "$ref": "#/definitions/partition" },
          "minItems": 1
        },
        "encryption": { "$ref": "#/definitions/encryption" },
        "hooks": { "$ref": "#/definitions/hooks" },
        "user_enrollment": { "$ref": "#/definitions/user_enrollment" }
      },
      "required": ["schema_version", "target_selection", "partitions"]
    },
    "target_selection": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "strategy": {
          "type": "string",
          "enum": ["largest_disk", "by_model", "by_serial", "by_size_min_gib", "prompt"]
        },
        "model": { "type": "string", "minLength": 1 },
        "serial": { "type": "string", "minLength": 1 },
        "size_min_gib": { "type": "integer", "minimum": 1 }
      },
      "required": ["strategy"]
    },
    "partition": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "label": { "type": "string", "minLength": 1, "maxLength": 36 },
        "partlabel": { "type": "string", "minLength": 1, "maxLength": 36 },
        "fslabel": { "type": "string", "minLength": 1, "maxLength": 36 },
        "role": { "type": "string", "enum": ["esp", "boot", "root", "home", "swap", "data"] },
        "size": { "type": "string", "pattern": "^([1-9][0-9]*(MiB|GiB)|rest)$" },
        "filesystem": { "type": "string", "enum": ["vfat", "ext4", "btrfs", "xfs", "swap"] },
        "mount": { "type": "string", "pattern": "^/.*" },
        "options": { "$ref": "#/definitions/string_list" },
        "flags": { "$ref": "#/definitions/string_list" },
        "subvolumes": { "$ref": "#/definitions/string_list" }
      },
      "required": ["label", "role", "size", "filesystem"]
    },
    "encryption": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "root": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "method": { "type": "string", "enum": ["luks2"] },
            "prompt": { "type": "boolean" }
          },
          "required": ["method"]
        }
      }
    },
    "hooks": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "pre_install": { "$ref": "#/definitions/hook_list" },
        "post_install": { "$ref": "#/definitions/hook_list" },
        "pre_update": { "$ref": "#/definitions/hook_list" },
        "post_update": { "$ref": "#/definitions/hook_list" }
      }
    },
    "user_enrollment": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "enabled": { "type": "boolean", "default": false },
        "default_auth": { "type": "string", "enum": ["fido2", "password"], "default": "fido2" },
        "shell": { "type": "string", "pattern": "^/.*" },
        "groups": { "$ref": "#/definitions/string_list" },
        "recovery_key": { "type": "boolean", "default": true },
        "fido2_with_client_pin": { "type": "boolean", "default": true }
      }
    },
    "hook_list": {
      "type": "array",
      "items": { "$ref": "#/definitions/hook" }
    },
    "hook": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "run": { "type": "string", "minLength": 1 },
        "enable_unit": { "type": "string", "minLength": 1 },
        "disable_unit": { "type": "string", "minLength": 1 },
        "mask_unit": { "type": "string", "minLength": 1 },
        "copy": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "from": { "type": "string", "pattern": "^/.*" },
            "to": { "type": "string", "pattern": "^/.*" }
          },
          "required": ["from", "to"]
        },
        "write_file": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "path": { "type": "string", "pattern": "^/.*" },
            "content": { "type": "string" },
            "mode": { "type": "string", "pattern": "^0[0-7]{3}$" }
          },
          "required": ["path", "content"]
        }
      },
      "oneOf": [
        { "required": ["run"] },
        { "required": ["enable_unit"] },
        { "required": ["disable_unit"] },
        { "required": ["mask_unit"] },
        { "required": ["copy"] },
        { "required": ["write_file"] }
      ]
    }
  }
}
