Git Product home page Git Product logo

nixpkgs-lint's Introduction

Semantic linter for Nixpkgs using tree-sitter ๐ŸŒณ + โ„๏ธ

asciicast

This is a semantic linter for Nixpkgs that uses tree-sitter. Currently we have the following detections:

  • cmake, makeWrapper, pkg-config in buildInputs
  • redundant packages from stdenv in nativeBuildInputs

Features

  • Fast: lints all of Nixpkgs in under 3 seconds
  • Semantic linting: forget about hacking up regexes, we run queries directly on parse trees created by tree-sitter
  • Syntax-aware: nixpkgs-lint can easily handle multi-line expressions, eliminates false-positives from strings and comments and gives exact spans for matches
  • Robust: lint Nix files even in the presence of syntax errors
  • Hackable: create your own lints by writing queries or Rust code

Usage

To use without installing, run nix run github:nix-community/nixpkgs-lint.

The tool will recurse through every .nix file in the provided path(,s).

$ nix build 
$ ./result/bin/nixpkgs-lint <files or directories>

Motivation

Why another linter? My motivation for this was spawned after doing a series of treewide PRs such as moving cmake to buildInputs. The strategy was similar each time; write some shell one-liner to go through every file (27,000+ of them) in Nixpkgs and find some anti-pattern to fix. However, this is quickly problematic for multiple reasons:

  • it is hard to account for multi-line expressions
  • it is hard to filter out false positives
  • it is hard to query for more complex features

In general discussions on IRC and Matrix, a more AST-aware approach to linting was viewed favorably but not many people took it on, despite the availability of Nix parsers in various languages. I have some subjective reasons myself:

  • need to learn the AST representation in the respective library
  • need to traverse the AST with a query
  • need to locate this information back to a source location

Often one or more of these would be pretty involved. Furthermore, it locks you into a specific parser (which may or may not have provided source information, parse all things correctly, etc.).

Enter tree-sitter. The Nix grammar for tree-sitter has been well-tested and tree-sitter having bindings in several languages gives you options in how to work with the resulting AST. You also get things like a location-annotated AST and error recovery for free.

License

This repository is licensed under the MIT license.

nixpkgs-lint's People

Contributors

artturin avatar ckiee avatar dependabot[bot] avatar siraben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.