Skip to content

Conversation

@albertodvp
Copy link

@albertodvp albertodvp commented Nov 8, 2025

Open Source Saturday

Summary

This PR introduces a Nix flake-based infrastructure to unify tooling
management, build processes, and code formatting across the project.

Motivation

Currently, the project has fragmented tooling management:

  • mise.toml - defines tool versions (10 tools)
  • Makefile - 24 shell scripts with dual implementations (native +
    Docker)
  • Docker images - multiple images for linting/formatting
  • Manual setup - developers need to install tools individually

This creates several problems:

  • Inconsistent tool versions across environments
  • Duplicate logic between native and Docker targets
  • No automated code formatting enforcement
  • Complex onboarding for new contributors

Add Nix flake with flake-parts for building and developing go-jsonschema:
- Package definition with buildGoModule
- Test suite as a check derivation
- Development shell with all required tools from mise.toml:
  - golang, golangci-lint, goreleaser
  - hadolint, markdownlint-cli2, shellcheck, shfmt
  - yamllint, yq, jsonlint, checkmake
  - adr-tools, gofumpt, jq

This provides reproducible development environments across all systems.
Add comprehensive Nix check derivations to flake.nix:

Test checks:
- go-jsonschema-tests: Run test suite with coverage output

Lint checks:
- lint-golang: golangci-lint validation
- lint-dockerfile: hadolint validation
- lint-json: jsonlint validation
- lint-makefile: checkmake validation
- lint-markdown: markdownlint-cli2 validation
- lint-shell: shellcheck validation
- lint-yaml: yamllint validation

Build checks:
- build-goreleaser: goreleaser build test

These checks can be run with 'nix flake check' and provide the foundation
for CI automation. Each check is a separate derivation that can be built
and cached independently.
Integrate treefmt-nix for unified code formatting:

Formatters configured:
- Nix files (alejandra)
- Shell scripts (shfmt with 2-space indent, -ci, -sr flags)
- Markdown (markdownlint-cli2 with project rules from .rules/)

Disabled formatters to avoid test data changes:
- Go formatters (gofmt, gofumpt, goimports)
- JSON formatter (jq)
- YAML formatter (yq) - causes file timestamp issues

Features:
- Run 'nix fmt' to format all supported files
- Set as default formatter output
- treefmt.build.wrapper available in devShell
- Excludes .direnv/, result/, .git/ from formatting

This provides a single command to format code across multiple languages
while respecting project-specific rules and test data integrity.
Integrate git-hooks.nix for automatic code formatting on commit:

Pre-commit hook configuration:
- Runs treefmt on staged files before each commit
- Auto-installs when entering 'nix develop'
- Formats code automatically (no --fail-on-change)
- Added to flake checks for CI validation

Changes:
- Added git-hooks.nix flake input
- Configured treefmt hook in pre-commit.settings
- Added pre-commit.installationScript to devShell hook
- Added .pre-commit-config.yaml to .gitignore

This ensures code is always formatted before commits, reducing review
friction and maintaining consistent code style across contributions.
Add automated CI workflow using Nix flake infrastructure:

Workflow steps:
1. Install Nix with flake support
2. Setup Cachix (devenv cache) for faster builds
3. Check code formatting with 'nix fmt --check'
4. Run all flake checks with 'nix flake check'
5. Build package with 'nix build'
6. Upload test coverage to Codecov

Benefits:
- Single workflow validates all code quality (tests, lints, builds)
- Cachix caching speeds up CI runs
- Hermetic builds - same results as local development
- Runs on ubuntu-24.04 with same concurrency controls as existing workflow

This provides an alternative to the mise-based development workflow,
with all checks unified under 'nix flake check'.
Apply treefmt formatting across the codebase:

Files formatted:
- README.md: Fixed markdown linting issues
  - MD059: Use descriptive link text instead of [here]
  - MD013: Wrap long lines to 120 characters
  - MD040: Add language identifier to code fence
- .goreleaser.yaml: YAML formatting (2-space indent)
- codecov.yml: YAML formatting (2-space indent)
- tests/data/.../gopkgYAMLv3AdditionalProperties.yaml: YAML formatting

This demonstrates the formatting tools are working correctly and brings
the codebase into compliance with the configured rules.
Add support for building and testing with multiple Go versions:

Flake changes:
- Refactored package and test derivations into reusable functions
- Added go-jsonschema-go124 and go-jsonschema-go125 packages
- Added tests-go124 and tests-go125 check derivations
- Extracted cleanSrc for code deduplication

GitHub Actions workflow:
- Added matrix strategy to test Go 1.24 and 1.25
- Split workflow into test-go-versions and qa jobs
- Upload coverage only from Go 1.25 tests
- Fail-fast disabled to test all versions

This ensures compatibility across supported Go versions and catches
version-specific issues early in CI.
Remove branch restriction from nix workflow to run on all pushes,
not just main branch. This ensures CI validation runs on feature
branches and pull requests.
@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@90a1a47). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pkg/yamlutils/yaml.go 40.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #487   +/-   ##
=======================================
  Coverage        ?   40.92%           
=======================================
  Files           ?       66           
  Lines           ?     5625           
  Branches        ?        0           
=======================================
  Hits            ?     2302           
  Misses          ?     3047           
  Partials        ?      276           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- use buildGoModule for build-goreleaser check with vendor support
- skip network-dependent steps (before hook, docker builds) in goreleaser
- split test checks into main module and integration tests with separate vendorHash
- configure lint-golang to use vendored dependencies via --modules-download-mode
- exclude tests/ directory from main module linting (separate go module)

These changes ensure all Nix checks pass in isolated sandbox without network access.
Document Nix setup for reproducible development:
- quick start commands (develop, check, build, fmt)
- benefits: hermetic testing, multi-version support, no docker dependency
- explain why Nix was introduced (reproducible builds, consistent environments)
Apply Go 1.18+ improvements and performance optimizations:
- replace interface{} with any for modern Go syntax
- use strings.Builder instead of += in loops for better performance
- replace reflect.TypeOf with reflect.TypeFor for type safety
- use strings.CutPrefix instead of HasPrefix + TrimPrefix
- simplify conditionals using max() builtin
- replace manual loops with slices.Contains

These changes improve code readability, performance, and align with current Go best practices.
@albertodvp albertodvp force-pushed the feat/nix-infrastructure branch from 84cbe9d to 49090b4 Compare November 8, 2025 21:09
@albertodvp albertodvp changed the title build: add Nix flake infrastructure with unified tooling and formatting WIP: build: add Nix flake infrastructure with unified tooling and formatting Nov 8, 2025
- test both Go versions (1.24, 1.25) on x86_64-linux and aarch64-darwin
- fix flake attribute paths (use checks.<system>.tests-go* instead of packages)
- add proper system-aware build paths (packages.<system>.go-jsonschema-go*)
- restrict codecov upload to x86_64-linux only to avoid duplicate reports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant