Project templates.
silk init creates a new package from a template: a directory of a GitHub repository that holds a silk.toml. Without --template, it uses the official default template, so even a plain silk init fetches one. This page covers naming a template, listing templates, what silk init copies and changes, and how to write a template of your own, including the [[post-init]] commands a template runs once in the new project.
Creating a project from a template#
silk init site --template acme/starters/webInitialized Silk project site at /home/you/site from template acme/starters/web (491fc724043f612c986fe5edb0a5af87bb58e148)-t is short for --template. The last part of the line is the full hash of the commit the files came from, so you can tell later which version of the template a project started from.
silk init with no --template uses silk-lang/examples/default:
silk init helloInitialized Silk project hello at /home/you/hello from template silk-lang/examples/default (3d330376eba6f634d1ca384269e46bc3c96727d8)| Form | Creates |
|---|---|
silk init hello | A program from the official default template. Needs git and the network |
silk init hello -t <template> | A package or a workspace from that template |
silk init hello -t minimal | The same program as default, written by the compiler itself. Works offline |
silk init --lib util | A library, written by the compiler. Works offline |
silk init --workspace ws | A workspace with one starter member, written by the compiler. Works offline |
--template cannot be combined with --lib or --workspace, because the template decides what it creates:
silk init: `--template` cannot be combined with `--lib` or `--workspace`: the template decides what it creates--name works with a package template as it does without one. A workspace template has no package to name, so --name is refused there.
Naming a template#
A template is written <owner>/<repository>[/<directory>...][@<revision>]:
| Written | Means |
|---|---|
hello | One segment: the directory hello of the official examples, silk-lang/examples/hello |
acme/starters | Two segments: the root of the repository acme/starters |
acme/starters/web | Three or more: the directory web inside acme/starters |
acme/starters/web@v2 | The same directory at the branch or tag v2 |
acme/starters@491fc724043f612c986fe5edb0a5af87bb58e148 | The root at that commit |
Two segments always name a repository's root, never a directory of the official examples. The revision is a branch, a tag or a full commit hash; without one, silk init uses the repository's default branch.
A name it cannot read exits 2:
Template revision `` must be a branch, a tag or a full commit hashA template that cannot be fetched, or that names no directory of its repository, exits 1:
Cannot fetch template acme/starters/web@nope: git fetch failed: fatal: couldn't find remote ref nope
Check the template's name and your network connection, or add `-t minimal` to create a minimal project offlineTemplate silk-lang/examples/Bad Name names no directory of its repository; `silk init --list` shows the official templatesListing templates#
silk init --list (or -l) lists the official templates with their descriptions:
silk init --listTemplates in silk-lang/examples (3d330376eba6f634d1ca384269e46bc3c96727d8):
default A program that logs a greeting, what `silk init` creates
hello A program that logs a greeting
library A library package with one function and its test
Create a project from one with `silk init <path> --template <name>`Give it a template name to list another repository, or a directory inside one:
silk init --list acme/startersTemplates in acme/starters (491fc724043f612c986fe5edb0a5af87bb58e148):
acme/starters/web A web server starter
Create a project from one with `silk init <path> --template <name>`Every directory with a silk.toml is a template, unless it is inside another template, so a workspace template's members are not listed on their own. Each is listed by the name --template takes, with the description from its [package] table.
What silk init does#
It fetches the repository with the
giton yourPATH, without history, into a scratch directory below$SILK_HOME/git(~/.silk/gitwhenSILK_HOMEis unset). The scratch directory is removed afterwards.It copies every file of the template's directory into the project directory, keeping executable bits and symbolic links. The repository's
.gitdirectory is not copied.It edits the new
silk.toml:In a package template,
[package].namebecomes the project's name (the directory name, or--name),silk-versionbecomes the running compiler's release, anddescriptionis removed, because it describes the template and not your project.In a workspace template, a
[workspace].silk-versionpin becomes the running compiler's release. The members are copied as they are.Every
[[post-init]]entry is removed, so it never runs again.
A workspace, or a package outside a workspace, gets
/build/in its.gitignore. A.gitignorefrom the template is kept, with the line added when it lacks it.It runs the template's
[[post-init]]entries in order, in the project directory.Inside a workspace, it adds the new package to the root's
members, assilk initalways does.
silk init never overwrites a file. If the template holds a file that already exists in the project directory, it stops before writing anything and exits 2:
Refusing to overwrite existing project file /home/you/site/scripts/setupWhen any later step fails, including a [[post-init]] entry, silk init removes every file and directory it created and restores a .gitignore it changed, so a failed run leaves the directory as it found it.
Writing a template#
A template is any directory of a GitHub repository with a silk.toml, a package or a workspace. Push it, and silk init -t <owner>/<repository>/<directory> can use it. The repository must be one git can fetch: a public repository, or a private one your git has credentials for.
starters/
web/
silk.toml
src/
main.silk
scripts/
setup[package]
name = "web"
version = "0.1.0"
description = "A web server starter"
[[post-init]]
run = ["git", "init", "--quiet"]
[[post-init]]
run = ["scripts/setup", "--greeting", "hello"]
arguments = trueKeep the template itself a working package: name can be anything, because silk init replaces it, and silk check in the template's directory checks it like any other package. Builds read [[post-init]] only to check its shape, so a template builds with its entries in place.
description is what silk init --list shows. It is removed from every project made from the template.
[[post-init]]#
A [[post-init]] entry names one command that silk init runs after it has copied the template, for adjustments that copying cannot make: renaming a module after the package, filling in a placeholder, or printing next steps. Entries run in the order they are written.
| Key | Value | Meaning |
|---|---|---|
run | array of strings | The program, then its arguments. A program with a / is a path below the template's directory, without . or .. segments; a bare name is searched on /usr/local/bin:/usr/bin:/bin. |
arguments | boolean | When true, the entry also gets every argument given to silk init after --. Default false. |
An entry with any other key, an empty run, or an arguments that is not true or false makes the template's silk.toml invalid, and silk init refuses the template.
Each command:
Runs in the new project's directory, after every file has been copied, inside a sandbox (see below).
Gets only four environment variables:
PATH=/usr/local/bin:/usr/bin:/bin,HOMEandTMPDIR, which both name a scratch directory, andSILK_VERSION, the release of the compiler that is creating the project, such as0.1.0-dev.56+f7d93cff9716.Gets the arguments in its
run, and nothing else, unless it setsarguments = true.Must exit 0.
What the commands print to standard output and standard error is shown before the Initialized line.
The sandbox#
A template is someone else's code, so its entries run in the same operating-system sandbox as the tool steps of build.silk: Landlock on Linux 6.7 and newer, and sandbox-exec on macOS. An entry can read the system's programs and libraries and the new project. It can write only the new project and its scratch HOME, and it cannot open TCP connections. So a template cannot read your other files, reach the network, or change anything outside the project it creates. A step that needs the network, such as installing packages, belongs in the instructions the template prints, for you to run yourself.
On a host with no sandbox, such as Windows or an older Linux kernel, silk init refuses a template that has [[post-init]] entries, before it writes anything, and exits 1:
Template acme/starters/web has [[post-init]] entries, and this host has no sandbox to run them in (Linux 6.7 with Landlock, or macOS); add --unsandboxed-post-init to run them with your full access--unsandboxed-post-init runs the entries anyway, with your environment and your full access, after a warning:
silk: warning: running the template's [[post-init]] entries without a sandbox (--unsandboxed-post-init)Pass it only for a template you trust. A template without entries needs no sandbox.
Arguments#
An entry with arguments = true gets the arguments given to silk init after --, after its own. With the template above, this command:
silk init site -t acme/starters/web -- --port 8080runs git init --quiet as written, then scripts/setup like this:
scripts/setup --greeting hello --port 8080Arguments after -- let a template take options from the person running silk init. Only the entries that set arguments = true see them, so an entry that runs a program you do not control, such as git init, keeps exactly the arguments you wrote. A template with no such entry refuses arguments after -- before it writes anything, rather than dropping them:
Template acme/starters/web takes no arguments after `--`: no [[post-init]] entry of its silk.toml sets `arguments = true`SILK_VERSION lets a script pick versions that match the compiler:
#!/bin/sh
# $1 and $2 are the entry's own arguments; the rest came after `--`.
greeting=$2
shift 2
echo "Created with Silk $SILK_VERSION, options: $*"When an entry fails#
A command that exits with a status other than 0, or that cannot be started, stops silk init. The project is removed and silk init exits 1:
silk: scripts/setup --greeting hello failed with status 1
silk: the template's [[post-init]] entry failed, so the project was removedA program that cannot be found is named with where it was looked for:
silk: [[post-init]][0]: cannot find the program scripts/setup in the templateScripts in a template#
The script files of a template are copied into the project like any other file, so they stay there after silk init. Commit the executable bit with the script (git update-index --chmod=+x scripts/setup), because silk init keeps the bits it finds and does not add any. Start a script with a #! line whose interpreter is in /usr/local/bin, /usr/bin or /bin, such as #!/bin/sh, because those are the only directories on the sandbox's PATH.
A script that should not stay in the project can remove itself as its last step.
See also#
Tasks and build graphs, The manifest, Workspaces; init in the Command-line reference.