fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . vscode ; 8 9vscodePname = cfg . package . pname ; 10vscodeVersion = cfg . package . version ; 11 12jsonFormat = pkgs . formats . json { }; 13 14configDir = { 15"vscode" = "Code" ; 16"vscode-insiders" = "Code - Insiders" ; 17"vscodium" = "VSCodium" ; 18}.${ vscodePname }; 19 20extensionDir = { 21"vscode" = "vscode" ; 22"vscode-insiders" = "vscode-insiders" ; 23"vscodium" = "vscode-oss" ; 24}.${ vscodePname }; 25 26userDir = if pkgs . stdenv . hostPlatform . isDarwin then 27"Library/Application Support/ ${ configDir } /User" 28else 29" ${ config . xdg . configHome } / ${ configDir } /User" ; 30 31configFilePath = " ${ userDir } /settings.json" ; 32tasksFilePath = " ${ userDir } /tasks.json" ; 33keybindingsFilePath = " ${ userDir } /keybindings.json" ; 34 35snippetDir = " ${ userDir } /snippets" ; 36 37# TODO: On Darwin where are the extensions? 38extensionPath = ". ${ extensionDir } /extensions" ; 39 40extensionJson = pkgs . vscode-utils . toExtensionJson cfg . extensions ; 41extensionJsonFile = pkgs . writeTextFile { 42name = "extensions-json" ; 43destination = "/share/vscode/extensions/extensions.json" ; 44text = extensionJson ; 45}; 46 47mergedUserSettings = cfg . userSettings 48// optionalAttrs ( ! cfg . enableUpdateCheck ) { "update.mode" = "none" ; } 49// optionalAttrs ( ! cfg . enableExtensionUpdateCheck ) { 50"extensions.autoCheckUpdates" = false ; 51}; 52in { 53imports = [ 54( mkChangedOptionModule [ "programs" "vscode" "immutableExtensionsDir" ] [ 55"programs" 56"vscode" 57"mutableExtensionsDir" 58] ( config :! config . programs . vscode . immutableExtensionsDir )) 59]; 60 61options = { 62programs . vscode = { 63enable = mkEnableOption "Visual Studio Code" ; 64 65package = mkOption { 66type = types . package ; 67default = pkgs . vscode ; 68defaultText = literalExpression "pkgs.vscode" ; 69example = literalExpression "pkgs.vscodium" ; 70description = '' 71Version of Visual Studio Code to install. 72'' ; 73}; 74 75enableUpdateCheck = mkOption { 76type = types . bool ; 77default = true ; 78description = '' 79Whether to enable update checks/notifications. 80'' ; 81}; 82 83enableExtensionUpdateCheck = mkOption { 84type = types . bool ; 85default = true ; 86description = '' 87Whether to enable update notifications for extensions. 88'' ; 89}; 90 91userSettings = mkOption { 92type = jsonFormat . type ; 93default = { }; 94example = literalExpression '' 95{ 96"files.autoSave" = "off"; 97"[nix]"."editor.tabSize" = 2; 98} 99'' ; 100description = '' 101Configuration written to Visual Studio Code's 102{file}`settings.json`. 103'' ; 104}; 105 106userTasks = mkOption { 107type = jsonFormat . type ; 108default = { }; 109example = literalExpression '' 110{ 111version = "2.0.0"; 112tasks = [ 113{ 114type = "shell"; 115label = "Hello task"; 116command = "hello"; 117} 118]; 119} 120'' ; 121description = '' 122Configuration written to Visual Studio Code's 123{file}`tasks.json`. 124'' ; 125}; 126 127keybindings = mkOption { 128type = types . listOf ( types . submodule { 129options = { 130key = mkOption { 131type = types . str ; 132example = "ctrl+c" ; 133description = "The key or key-combination to bind." ; 134}; 135 136command = mkOption { 137type = types . str ; 138example = "editor.action.clipboardCopyAction" ; 139description = "The VS Code command to execute." ; 140}; 141 142when = mkOption { 143type = types . nullOr ( types . str ); 144default = null ; 145example = "textInputFocus" ; 146description = "Optional context filter." ; 147}; 148 149# https://code.visualstudio.com/docs/getstarted/keybindings#_command-arguments 150args = mkOption { 151type = types . nullOr ( jsonFormat . type ); 152default = null ; 153example = { direction = "up" ; }; 154description = "Optional arguments for a command." ; 155}; 156}; 157}); 158default = [ ]; 159example = literalExpression '' 160[ 161{ 162key = "ctrl+c"; 163command = "editor.action.clipboardCopyAction"; 164when = "textInputFocus"; 165} 166] 167'' ; 168description = '' 169Keybindings written to Visual Studio Code's 170{file}`keybindings.json`. 171'' ; 172}; 173 174extensions = mkOption { 175type = types . listOf types . package ; 176default = [ ]; 177example = literalExpression "[ pkgs.vscode-extensions.bbenoist.nix ]" ; 178description = '' 179The extensions Visual Studio Code should be started with. 180'' ; 181}; 182 183mutableExtensionsDir = mkOption { 184type = types . bool ; 185default = true ; 186example = false ; 187description = '' 188Whether extensions can be installed or updated manually 189or by Visual Studio Code. 190'' ; 191}; 192 193languageSnippets = mkOption { 194type = jsonFormat . type ; 195default = { }; 196example = { 197haskell = { 198fixme = { 199prefix = [ "fixme" ]; 200body = [ "$LINE_COMMENT FIXME: $0" ]; 201description = "Insert a FIXME remark" ; 202}; 203}; 204}; 205description = "Defines user snippets for different languages." ; 206}; 207 208globalSnippets = mkOption { 209type = jsonFormat . type ; 210default = { }; 211example = { 212fixme = { 213prefix = [ "fixme" ]; 214body = [ "$LINE_COMMENT FIXME: $0" ]; 215description = "Insert a FIXME remark" ; 216}; 217}; 218description = "Defines global user snippets." ; 219}; 220}; 221}; 222 223config = mkIf cfg . enable { 224home . packages = [ cfg . package ]; 225 226home . file = mkMerge [ 227( mkIf ( mergedUserSettings != { }) { 228" ${ configFilePath } " . source = 229jsonFormat . generate "vscode-user-settings" mergedUserSettings ; 230}) 231( mkIf ( cfg . userTasks != { }) { 232" ${ tasksFilePath } " . source = 233jsonFormat . generate "vscode-user-tasks" cfg . userTasks ; 234}) 235( mkIf ( cfg . keybindings != [ ]) 236( let dropNullFields = filterAttrs ( _ :v :v != null ); 237in { 238" ${ keybindingsFilePath } " . source = 239jsonFormat . generate "vscode-keybindings" 240( map dropNullFields cfg . keybindings ); 241})) 242( mkIf ( cfg . extensions != [ ]) ( let 243subDir = "share/vscode/extensions" ; 244 245# Adapted from https://discourse.nixos.org/t/vscode-extensions-setup/1801/2 246toPaths = ext : 247map ( k :{ " ${ extensionPath } / ${ k } " . source = " ${ ext } / ${ subDir } / ${ k } " ; }) 248( if ext ?vscodeExtUniqueId then 249[ ext . vscodeExtUniqueId ] 250else 251builtins . attrNames ( builtins . readDir ( ext + "/ ${ subDir } " ))); 252in if cfg . mutableExtensionsDir then 253mkMerge ( concatMap toPaths cfg . extensions 254++ lib . optional ( lib . versionAtLeast vscodeVersion "1.74.0" ) { 255# Whenever our immutable extensions.json changes, force VSCode to regenerate 256# extensions.json with both mutable and immutable extensions. 257" ${ extensionPath } /.extensions-immutable.json" = { 258text = extensionJson ; 259onChange = '' 260$DRY_RUN_CMD rm $VERBOSE_ARG -f ${ extensionPath } /{extensions.json,.init-default-profile-extensions} 261$VERBOSE_ECHO "Regenerating VSCode extensions.json" 262$DRY_RUN_CMD ${ getExe cfg . package } --list-extensions > /dev/null 263'' ; 264}; 265}) 266else { 267" ${ extensionPath } " . source = let 268combinedExtensionsDrv = pkgs . buildEnv { 269name = "vscode-extensions" ; 270paths = cfg . extensions 271++ lib . optional ( lib . versionAtLeast vscodeVersion "1.74.0" ) 272extensionJsonFile ; 273}; 274in " ${ combinedExtensionsDrv } / ${ subDir } " ; 275})) 276 277( mkIf ( cfg . globalSnippets != { }) 278( let globalSnippets = " ${ snippetDir } /global.code-snippets" ; 279in { 280" ${ globalSnippets } " . source = 281jsonFormat . generate "user-snippet-global.code-snippets" 282cfg . globalSnippets ; 283})) 284 285( lib . mapAttrs' ( language :snippet : 286lib . nameValuePair " ${ snippetDir } / ${ language } .json" { 287source = jsonFormat . generate "user-snippet- ${ language } .json" snippet ; 288}) cfg . languageSnippets ) 289]; 290}; 291}