NiXium-org / NiXium

Open-Source Infrastructure as Code Management Solution for Multiple Systems designed to be reliable in mission-critical tasks on paranoid and high-security environment.
European Union Public License 1.2
25 stars 3 forks source link

Use deadnix to scan nix files for dead code and automatically remove unused code #66

Open Tanvir1337x opened 5 months ago

Tanvir1337x commented 5 months ago

ref: deadnix See deadnix --help for a full list of options.

To highlight the dead codes in your nix code:

deadnix

It recursively finds nix files and raises dead code issues for the provided directory.

To fix several such occurrences:

deadnix -e
Tanvir1337x commented 5 months ago

Pre-commit Hook

Add the following to your project's .pre-commit-config.yaml:

- repo: https://github.com/astro/deadnix
    rev: v1.2.1 # Replace with the relevant version tag/commit ID
    hooks:
      - id: deadnix
        args: [--edit] # Automatically modify files
        stages: [commit]
Tanvir1337x commented 5 months ago

Github Actions Workflow

Add the following to your project's .github/workflows/deadnix.yml:

name: Dead nix code analysis
on:
  push:
    paths:
      - "**.nix"
  workflow_dispatch:

jobs:
  deadnix:
    name: Deadnix
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v25
      - uses: cachix/cachix-action@v14
        with:
          name: deadnix
      - uses: astro/deadnix-action@main

The workflow runs automatically on every push to the repository that includes changes to .nix files.

It can also be triggered manually via the "Workflow dispatch" feature in the GitHub Actions tab of the repository. It provides flexibility to run the formatting process as needed outside of the regular push events.

Possible to apply fixes with deadnix -e and push it with something like:

- name: Push changes
  uses: stefanzweifel/git-auto-commit-action@v4
  with:
    commit_message: "chore: fixing dead code issues with deadnix"

or for PR/MR we could use something like peter-evans/create-pull-request.

Tanvir1337x commented 5 months ago

I'm using this workflow: deadnix.yml