How to add option for configuration.nix

I am new to nixos and trying to create a local configurable to config if add vim extension to vscode package.

{ config, pkgs, lib, …}:
with pkgs;
let
options.vscode = {
vim = mkOption {
type= types.bool;
default = false;
description = “If add vscode vim extension”;
};
};

extensions = (with pkgs.vscode-extensions; [
ms-vscode.cpptools
ms-python.python
]) ++ (config.vscode.vim [ pkgs.vscode-extensions.vscodevim.vim])

vscode-with-extensions = pkgs.vscode-with-extensions.override {
vscodeExtensions = extensions;
};
in {
environment.systemPackages = with pkgs; [
vscode-with-extensions
];
}

I was trying to use config.vscode.vim = true/false to add or ingore vim plugin. However it does not work.

I searched some wiki page and tutorial, it mentions the option = {} is declaration however config = {} is definition. However, I found most example seems tend to use the declared option to init the already existing system options. I am not sure how to fit the case to my usage.

What the best way to add some simple Flags in configuration.nix?

Thanks

It is hard to read your code, can you please use markdown (either fenced or indented) code blocks?

Though what I see is, you set options within the let, though you should set it in the returned attribute set.

Similarily you should use config within that attribute set to actually set configuration.

The NixOS manual about modules explains how you need to write your modules and give some examples.

1 Like

I guess this would do the trick:

{ config, pkgs, lib, ...}:

# Used to import optionals function in scope
with lib;

let
  # Just to shorten the different calls
  cfg = config.vscode;

  extensions = (with pkgs.vscode-extensions; [
    ms-vscode.cpptools
    ms-python.python
  ]) ++ (optionals cfg.enableVim [ pkgs.vscode-extensions.vscodevim.vim ])

  vscode-with-extensions = pkgs.vscode-with-extensions.override {
    vscodeExtensions = extensions;
  };
in
{
  # Option declaration
  options.vscode = {
    # This helper function generates what you wrote
    enableVim = mkEnableOption “vim extension for vscode”;
  };

  # Now the result of the configuration:
  config = {
    environment.systemPackages = [
      vscode-with-extensions
    ];
  }
}
1 Like

Thanks @freezeboy it works like a charm.

thanks for pointing out the format issue, will do next time!