nixpkgs/pkgs/applications/editors/vscode/extensions/updateSettings.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.3 KiB
Nix
Raw Normal View History

# 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 exist
, createIfDoesNotExists ? true
, vscodeSettingsFile ? ".vscode/settings.json"
, userSettingsFolder ? ""
, symlinkFromUserSetting ? false
}:
let
updateVSCodeSettingsCmd = ''
(
2020-08-30 23:17:48 +00:00
echo 'updateSettings.nix: Updating ${vscodeSettingsFile}...'
oldSettings=$(cat ${vscodeSettingsFile})
echo $oldSettings' ${builtins.toJSON settings}' | ${jq}/bin/jq -s add > ${vscodeSettingsFile}
)'';
createEmptySettingsCmd = ''mkdir -p .vscode && echo "{}" > ${vscodeSettingsFile}'';
fileName = builtins.baseNameOf vscodeSettingsFile;
symlinkFromUserSettingCmd = lib.optionalString symlinkFromUserSetting
'' && mkdir -p "${userSettingsFolder}" && ln -sfv "$(pwd)/${vscodeSettingsFile}" "${userSettingsFolder}/" '';
2020-08-30 23:17:48 +00:00
in
writeShellScriptBin ''vscodeNixUpdate-${lib.removeSuffix ".json" (fileName)}''
2020-08-30 23:17:48 +00:00
(lib.optionalString (settings != {})
(if createIfDoesNotExists then ''
[ ! -f "${vscodeSettingsFile}" ] && ${createEmptySettingsCmd}
${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
''
else ''[ -f "${vscodeSettingsFile}" ] && ${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
''
)
)