Skip to content

Commit

Permalink
feat(files): #7 Option to disable file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugosenari committed Feb 17, 2023
1 parent 040c91f commit 3c6bef6
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ This project is configured by module [project.nix](./project.nix)

# create .envrc for direnv
files.direnv.enable = true;

# disabe file creation when entering in the shell
# call devshell-files instead
# files.on-call = true;
}

```
Expand Down Expand Up @@ -602,6 +606,7 @@ To document our modules is simple, we just need to use `config.files.docs` as fo
files.docs."/gh-pages/src/modules/cmds.md".modules = [ ../modules/cmds.nix ];
files.docs."/gh-pages/src/modules/files.md".modules = [ ../modules/files.nix ];
files.docs."/gh-pages/src/modules/git.md".modules = [ ../modules/git.nix ];
files.docs."/gh-pages/src/modules/on-call.md".modules = [ ../modules/startup.nix ];
files.docs."/gh-pages/src/modules/gitignore.md".modules = [ ../modules/gitignore.nix ];
files.docs."/gh-pages/src/modules/hcl.md".modules = [ ../modules/hcl.nix ];
files.docs."/gh-pages/src/modules/json.md".modules = [ ../modules/json.nix ];
Expand Down Expand Up @@ -697,6 +702,7 @@ They are already included when we use this package.
- `files.cmds`, install packages from [nix repository](https://search.nixos.org/)
- `files.docs`, convert our modules file into markdown using [nmd](https://gitlab.com/rycee/nmd)
- `files.git`, configure git with file creation
- `files.on-call`, connfigure file to created only when devshell-files command is called, not on shell start
- `files.gitignore`, copy .gitignore from [templates](https://github.com/github/gitignore/)
- `files.hcl`, create HCL files with nix syntax
- `files.json`, create JSON files with nix syntax
Expand Down
1 change: 1 addition & 0 deletions examples/docs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
files.docs."/gh-pages/src/modules/cmds.md".modules = [ ../modules/cmds.nix ];
files.docs."/gh-pages/src/modules/files.md".modules = [ ../modules/files.nix ];
files.docs."/gh-pages/src/modules/git.md".modules = [ ../modules/git.nix ];
files.docs."/gh-pages/src/modules/on-call.md".modules = [ ../modules/startup.nix ];
files.docs."/gh-pages/src/modules/gitignore.md".modules = [ ../modules/gitignore.nix ];
files.docs."/gh-pages/src/modules/hcl.md".modules = [ ../modules/hcl.nix ];
files.docs."/gh-pages/src/modules/json.md".modules = [ ../modules/json.nix ];
Expand Down
1 change: 1 addition & 0 deletions examples/readme/modules/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ They are already included when we use this package.
- `files.cmds`, install packages from [nix repository](https://search.nixos.org/)
- `files.docs`, convert our modules file into markdown using [nmd](https://gitlab.com/rycee/nmd)
- `files.git`, configure git with file creation
- `files.on-call`, connfigure file to created only when devshell-files command is called, not on shell start
- `files.gitignore`, copy .gitignore from [templates](https://github.com/github/gitignore/)
- `files.hcl`, create HCL files with nix syntax
- `files.json`, create JSON files with nix syntax
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
./modules/toml.nix
./modules/yaml.nix
./modules/hcl.nix
./modules/startup.nix
./modules/git.nix
./modules/gitignore.nix
./modules/spdx.nix
Expand Down
5 changes: 3 additions & 2 deletions modules/dockerfile.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ let
cfg = config.files.dockerfile;
# directives = pkgs.callPackage ./dockerfile/directives.nix { inherit lib; };
toFile = name: value: {
source = pkgs.writeTextFile {
source = pkgs.writeTextFile {
name = (builtins.baseNameOf name);
text = value;
};
git-add = lib.mkIf config.files.git.auto-add true;
git-add = lib.mkIf config.files.git.auto-add true;
on-enter = lib.mkIf config.files.on-call (lib.mkDefault false);
};
in {
options.files.dockerfile = lib.mkOption {
Expand Down
5 changes: 3 additions & 2 deletions modules/file-format.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ let
value = builtins.toJSON value;
} ''yj ${yj-arg} < $valuePath > $out'';
toFile = name: value: {
source = gen name value;
git-add = lib.mkIf config.files.git.auto-add true;
source = gen name value;
git-add = lib.mkIf config.files.git.auto-add (lib.mkDefault true);
on-enter = lib.mkIf config.files.on-call (lib.mkDefault false);
};
in {
options.files.${format} = lib.mkOption {
Expand Down
8 changes: 8 additions & 0 deletions modules/file-type.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ in
To add this file to git repository after creation
'';
};

on-enter = mkOption {
type = types.nullOr types.bool;
default = true;
description = ''
This file will be created on enter in development shell
'';
};
};

config = {
Expand Down
16 changes: 8 additions & 8 deletions modules/files.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ let
else "";
toName = name: ".dsf${lib.strings.sanitizeDerivationName name}";
# Execute this script to update the project's files
copy-file = name: file: pkgs.writeShellScriptBin "${toName name}" ''
target="$PRJ_ROOT${file.target}"
${pkgs.coreutils}/bin/install -m 644 -D ${file.source} $target
copy-files = map (name: "source $DEVSHELL_DIR/bin/${toName name}") (builtins.attrNames files);
copy-files'= lib.mapAttrsToList (name: file: if file.on-enter then "source $DEVSHELL_DIR/bin/${toName name}" else "") files;
copy-file = name: file: pkgs.writeShellScriptBin "${toName name}" ''
${pkgs.coreutils}/bin/install -m 644 -D ${file.source} "$PRJ_ROOT${file.target}"
${chmod file}
${git-add file}
'';
cmd.command = builtins.concatStringsSep "\n" startups;
cmd.command = builtins.concatStringsSep "\n" copy-files;
cmd.help = "Recreate files";
cmd.name = "devshell-files";
opt.default = {};
opt.description = "Attribute set of files to create into the project root.";
opt.type = fileType "<envar>PRJ_ROOT</envar>";
startup.devshell-files.text = "$DEVSHELL_DIR/bin/devshell-files";
startups = map (name: "source $DEVSHELL_DIR/bin/${toName name}") (builtins.attrNames files);
startup.devshell-files.text = builtins.concatStringsSep "\n" copy-files';
in {
options.file = lib.mkOption opt;
config.commands = lib.mkIf (builtins.length startups > 0) [ cmd ];
config.commands = lib.mkIf (builtins.length copy-files > 0) [ cmd ];
config.devshell.packages = lib.mapAttrsToList copy-file files;
config.devshell.startup = lib.mkIf (builtins.length startups > 0) startup;
config.devshell.startup = lib.mkIf (builtins.length copy-files > 0) startup;
}
4 changes: 1 addition & 3 deletions modules/git.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{pkgs, config, lib, ...}:
let
cfg = config.files.git;
in {
{
options.files.git.auto-add = lib.mkEnableOption "auto add files to git after creation";
}
4 changes: 4 additions & 0 deletions modules/startup.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{pkgs, config, lib, ...}:
{
options.files.on-call = lib.mkEnableOption "Files will be created when devshell-files command is called instead of when start the shell";
}
5 changes: 3 additions & 2 deletions modules/text.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ let
format = "text";
cfg = config.files.${format};
toFile = name: value: {
git-add = lib.mkIf config.files.git.auto-add true;
source = pkgs.writeTextFile {
git-add = lib.mkIf config.files.git.auto-add true;
on-enter = lib.mkIf config.files.on-call (lib.mkDefault false);
source = pkgs.writeTextFile {
name = (builtins.baseNameOf name);
text = value;
};
Expand Down
4 changes: 4 additions & 0 deletions project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@

# create .envrc for direnv
files.direnv.enable = true;

# disabe file creation when entering in the shell
# call devshell-files instead
# files.on-call = true;
}

0 comments on commit 3c6bef6

Please sign in to comment.