Skip to content

Instantly share code, notes, and snippets.

@Melkor333
Created August 4, 2022 05:02
Show Gist options
  • Save Melkor333/4f2636fd1f0cb4f41619213b7b10bc97 to your computer and use it in GitHub Desktop.
Save Melkor333/4f2636fd1f0cb4f41619213b7b10bc97 to your computer and use it in GitHub Desktop.
Nix-snippets Oil script
#!/usr/bin/env oil
### TODO: Maybe define a Nix-Snippet standard and allow other snippet sources (from github & co)
setglobal SOURCES = [ './snippets' ]
setglobal DESTINATION = '/etc/nixos/snippets'
setglobal SUDO = 'sudo'
proc askVariables (snippet) {
### For each ${x} in the snippet, ask if the variable should be replaced by something (and by what)
echo
}
proc getSnippet (path, :snippet) {
var fileName = ${path##*/}
var snippetName = ${fileName%.*}
var newFile = $DESTINATION ++ "/" ++ $fileName
var status = ''
try test -f $path
if (_status !== 0) {
return 1
}
try test -f $newFile
if (_status !== 0) {
setvar status = "new"
} else {
#
#var lastLine = $(tail -1 $newFile)
var lastLine = "#ignored"
if ($lastLine ~ "#ignored") {
setvar status = "ignored"
} else {
# TODO: Lint both files before diffing them to minimize diff
var fileDiff = $(diff --suppress-common-lines --suppress-blank-empty -twB $path $newFile)
if [-z $fileDiff] {
setvar status = "uptodate"
} else {
setvar status = "diff"
}
}
}
setref snippet = { name: $fileName,
path: $path,
newFile: $newFile,
status: $status }
}
proc getSnippets (:snippetsOut) {
### List all Snippets which can be added to the config
# Under the hood:
# find all lines containing milkos.NAME.enable and return a list containing dicts
# Snippet dict contains:
# name -> Obvious
# hash -> A hash of the configuration
# state -> string of either: new, ignored, uptodate or diff
# -> new: Is not implemented in the config
# -> ignored: Has been looked at once, don't want to see it again (e.g. has been implemented
# -> uptodate: Is currently used and the same as
# -> diff: is currently used, but there is a diff to the config in use
# Optional: A diff
var fileList = %()
var snippets = []
for source in @SOURCES {
setvar fileList = fileList ++ %($source/*nix)
}
for file in @fileList {
runproc getSnippet $file :snippet
setvar snippets = snippets + [snippet]
}
setref snippetsOut = snippets
}
proc diffSnippet (snippet) {
### Print a diff between the currently used Snippet and its source
echo
}
proc applySnippet (snippet) {
### Apply the changes of the Snippet source
echo
}
proc ignoreSnippet (snippet) {
### Ignore a snippet now and for the future. Add the name to an ignorelist
echo
}
proc list {
getSnippets :snippets
for snippet in (snippets) {
# Gives an error at the *previous* line
if ($snippet[status] !== 'ignored') {
write -- "$[snippet->name]: $[snippet->status]"
}
# Works
#if ("$[snippet->status]" !== 'ignored') {
# write -- "$[snippet->name]: $[snippet->status]"
#}
}
}
proc diff {
for arg in @ARGV {
echo $arg
}
}
runproc @ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment