add updateSettings drv which will union nix settings configurations into the a vscode settings file

This commit is contained in:
Oren Rozen 2020-01-29 23:21:53 -05:00
parent 3d102d1c90
commit a535e7a04f
2 changed files with 39 additions and 0 deletions

View file

@ -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}''
))

View file

@ -0,0 +1,6 @@
with import <nixpkgs>{};
callPackage (import ./updateSettings.nix) {} {
settings = {
a = "fdsdf";
};
}