From a535e7a04ff11615f43a222ad496c6ffd39d176c Mon Sep 17 00:00:00 2001 From: Oren Rozen Date: Wed, 29 Jan 2020 23:21:53 -0500 Subject: [PATCH] add updateSettings drv which will union nix settings configurations into the a vscode settings file --- .../misc/vscode-extensions/updateSettings.nix | 33 +++++++++++++++++++ .../vscode-extensions/updateSettingsTest.nix | 6 ++++ 2 files changed, 39 insertions(+) create mode 100644 pkgs/misc/vscode-extensions/updateSettings.nix create mode 100644 pkgs/misc/vscode-extensions/updateSettingsTest.nix diff --git a/pkgs/misc/vscode-extensions/updateSettings.nix b/pkgs/misc/vscode-extensions/updateSettings.nix new file mode 100644 index 000000000000..e3b09582ffe3 --- /dev/null +++ b/pkgs/misc/vscode-extensions/updateSettings.nix @@ -0,0 +1,33 @@ +# Updates the vscode setting file base on a nix expression +# should run from the workspace root. +{ writeShellScriptBin +, lib +, jq +}: +##User Input +{ settings ? {} +# if marked as true will create an empty json file if does not exists +, createIfDoesNotExists ? true +, vscodeSettingsFile ? .vscode/settings.json +}: +let + #VSCode Settings file + vscodeSettingsFileStr = toString vscodeSettingsFile; + + updateVSCodeSettingsCmd = '' + ( + echo 'updateSettings.nix: Updating ${vscodeSettingsFileStr}...' + oldSettings=$(cat ${vscodeSettingsFileStr}) + echo $oldSettings' ${builtins.toJSON settings}' | ${jq}/bin/jq -s add > ${vscodeSettingsFileStr} + )''; + + createEmptySettingsCmd = ''mkdir -p .vscode && echo "{}" > ${vscodeSettingsFileStr}''; +in + writeShellScriptBin ''vscodeNixUpdate-${lib.removeSuffix ".json" (builtins.baseNameOf vscodeSettingsFileStr)}'' + (lib.optionalString (settings != {}) + (if createIfDoesNotExists then '' + [ ! -f "${vscodeSettingsFileStr}" ] && ${createEmptySettingsCmd} + ${updateVSCodeSettingsCmd} + '' + else ''[ -f "${vscodeSettingsFileStr}" ] && ${updateVSCodeSettingsCmd}'' + )) diff --git a/pkgs/misc/vscode-extensions/updateSettingsTest.nix b/pkgs/misc/vscode-extensions/updateSettingsTest.nix new file mode 100644 index 000000000000..097b9cad1661 --- /dev/null +++ b/pkgs/misc/vscode-extensions/updateSettingsTest.nix @@ -0,0 +1,6 @@ +with import {}; +callPackage (import ./updateSettings.nix) {} { + settings = { + a = "fdsdf"; + }; +}