Home

Awesome

Nix IDE

Adds Nix language support for Visual Studio Code.

Installation

Available on both the Visual Studio Marketplace and the Open VSX Registry.

You can also open the Command Palette (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> on Windows/Linux or <kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> on macOS) and enter ext install jnoortheen.nix-ide to install the extension, or download it from the latest release.

Features

The basic language integration is supported out of the box using nixpkgs-fmt and nix instantiate.

<details> <summary>Syntax Highlighting support</summary> <img src="./images/docs/nix-syntax-highlight.png" alt="syntax highlighting"/> </details> <details> <summary>Syntax highlighting of Nix code blocks inside `markdown` files also work.</summary> <img src="./images/docs/md-embed-nix.png" alt="embedded syntax highlighting"/> </details> <details> <summary>Syntax Errors are reported using `nix-instantiate`</summary> <img src="./images/docs/linting.png" alt="Screenshot of an error message tooltip"/> </details> <details> <summary> Auto-Formatting is handled by `nixpkgs-fmt` by default. </summary>

It can be changed by setting nix.formatterPath to any command which can accept file contents on stdin and return formatted text on stdout.

{
"nix.formatterPath": "nixpkgs-fmt"
    // "nix.formatterPath": "nixfmt"
    // "nix.formatterPath": ["treefmt", "--stdin", "{file}"]
    // "nix.formatterPath": ["nix", "fmt", "--", "--"] // using flakes with `formatter = pkgs.alejandra;`
}
</details>

LSP Plugin Support

Full language support can be enabled by using a language server. Generally, any Nix LSP implementation should work.

The following have been tested so far:

{
  "nix.enableLanguageServer": true,

  "nix.serverPath": "nil",
  // or
  "nix.serverPath": "nixd"
}
<details> <summary>Advanced settings </summary>

Pass settings to the language server via the serverSettings option.

{
  "nix.serverSettings": {
    "nil": {
      "diagnostics": {
        "ignored": ["unused_binding", "unused_with"]
      },
      "formatting": {
        "command": ["nixpkgs-fmt"]
      }
    }
  }
}
{
    "nix.serverSettings": {
        "nixd": {
            "formatting": {
                "command": [ "nixpkgs-fmt" ]
            },
            "options": {
                // By default, this entry will be read from `import <nixpkgs> { }`.
                // You can write arbitrary Nix expressions here, to produce valid "options" declaration result.
                // Tip: for flake-based configuration, utilize `builtins.getFlake`
                "nixos": {
                    "expr": "(builtins.getFlake \"/absolute/path/to/flake\").nixosConfigurations.<name>.options"
                },
                "home-manager": {
                    "expr": "(builtins.getFlake \"/absolute/path/to/flake\").homeConfigurations.<name>.options"
                },
                // Tip: use ${workspaceFolder} variable to define path
                "nix-darwin": {
                  "expr": "(builtins.getFlake \"${workspaceFolder}/path/to/flake\").darwinConfigurations.<name>.options"
                }
            }
        }
    }
}
</details>

Contributing

Development

There is direnv and nix-shell support so a dev environment can be created with the nix-shell command or a one-time direnv allow at the root of the repo.

Press F5 in VSCode to run an Extension Development Host instance with the extension installed.

TypeScript is used to develop the extension.

yarn install # install dependencies
yarn build   # build the extension

Releasing a new version

Complete .env with environment variables based on .env.template,

# this will generate changelog and will create a GitHub release. This will also trigger jobs to publish the extension.
yarn release

# to manually publish the extension
yarn env-cmd && yarn publish

Credits

Special thanks to: