Skip to content

Commit f06ac24

Browse files
committed
Add an 'init' step
Split the 'activate' script into 'init' and 'activate'. The goal is to later merge 'init', 'activate', 'open' and the handling of shell.nix into one script.
1 parent f0f456c commit f06ac24

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

modules/git.nix

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
with lib;
44

5-
let conf = config.git; in
5+
let
6+
conf = config.git;
7+
8+
sync_remotes = ''
9+
# Sync remotes
10+
${pkgs.bash}/bin/bash ${./sync_git_remotes.sh} <<"EOF"
11+
${concatStringsSep "\n" (mapAttrsToList (n: u: "${n} ${u}") conf.remotes)}
12+
EOF
13+
'';
14+
15+
in
616

717
{
818
options.git = {
@@ -32,26 +42,20 @@ let conf = config.git; in
3242
config = mkIf (conf.remotes != {}) {
3343
buildInputs = with pkgs; [ git ];
3444

35-
activation_script = ''
36-
# Init if not already
37-
did_init=0
38-
if ! [[ -e .git ]]; then git init; did_init=1; fi
45+
init_script = ''
46+
git init
47+
${sync_remotes}
48+
git fetch --all
49+
git checkout --track "${conf.main_remote}/${conf.main_branch}"
50+
'';
3951

40-
# Sync remotes
41-
${pkgs.bash}/bin/bash ${./sync_git_remotes.sh} <<"EOF"
42-
${concatStringsSep "\n" (mapAttrsToList (n: u: "${n} ${u}") conf.remotes)}
43-
EOF
52+
activation_script = ''
53+
${sync_remotes}
4454
4555
${if config.git.gitignore == "" then "" else ''
4656
# Gitignore
47-
ln -sf "${builtins.toFile "${config.name}-vimrc" config.git.gitignore}" .git/info/exclude
57+
ln -sf "${builtins.toFile "${config.name}-gitignore" conf.gitignore}" .git/info/exclude
4858
''}
49-
50-
# If init, fetch remotes
51-
if [[ $did_init -eq 1 ]]; then
52-
git fetch --all
53-
git checkout --track "${conf.main_remote}/${conf.main_branch}"
54-
fi
5559
'';
5660

5761
};

workspaces

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ open_workspace ()
4242
let ws = import workspaces_nix { inherit pkgs config; }; in
4343
ws.by_name { inherit wname; }') || wait_input_and_exit "Building of $wname in $2 failed."
4444

45-
mkdir -p "$p"
45+
if ! [[ -d "$p" ]]; then
46+
mkdir -p "$p"
47+
cd "$p"
48+
nix-shell -p "${buildInputs[@]}" --run workspace-init || wait_input_and_exit "Initialisation failed."
49+
fi
50+
4651
cd "$p"
4752
nix-shell -p "${buildInputs[@]}" --run workspace-activate || wait_input_and_exit "Activating failed."
4853

workspaces.nix

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ let
1111
description = "Workspace name. Defaults to the attribute name used to define it.";
1212
};
1313

14+
init_script = mkOption {
15+
type = types.lines;
16+
default = "";
17+
description = "Run when the workspace is activated for the first time.";
18+
};
19+
1420
activation_script = mkOption {
1521
type = types.lines;
1622
default = "";
@@ -66,10 +72,11 @@ let
6672
assert (builtins.hasAttr wname workspaces || throw "Workspace ${wname} not found");
6773
let
6874
w = builtins.getAttr wname workspaces;
75+
init = pkgs.writeShellScriptBin "workspace-init" w.init_script;
6976
activate = pkgs.writeShellScriptBin "workspace-activate" w.activation_script;
7077
open = pkgs.writeShellScriptBin "workspace-open" w.command;
7178
in
72-
w.buildInputs ++ [ activate open ];
79+
w.buildInputs ++ [ init activate open ];
7380
};
7481

7582
in

0 commit comments

Comments
 (0)