From d446f840304029df855f8d98d80b3aa5fb4ed1a9 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 9 Oct 2024 00:37:47 +0200 Subject: [PATCH 01/10] refactor: separate NixOS and Home-Manager configuration --- flake.nix | 124 ++++++++++++++++----------------------- home-manager/base.nix | 13 +--- home-manager/default.nix | 25 +++++--- nixos/default.nix | 28 +-------- taskfile.yaml | 12 +--- 5 files changed, 75 insertions(+), 127 deletions(-) diff --git a/flake.nix b/flake.nix index 8d89bce..4bc95ff 100644 --- a/flake.nix +++ b/flake.nix @@ -17,18 +17,46 @@ }; }; outputs = { self, nixpkgs, home-manager, nur, nixgl, sops-nix, ... }@inputs: -let - stateVersion = "23.11"; + let + stateVersion = "23.11"; + + allSystems = [ + "x86_64-linux" # 64bit AMD/Intel x86 + "aarch64-linux" # 64bit ARM Linux + ]; - allSystems = [ - "x86_64-linux" # 64bit AMD/Intel x86 - "aarch64-linux" # 64bit ARM Linux - ]; - - forAllSystems = fn: - nixpkgs.lib.genAttrs allSystems - (system: fn { pkgs = import nixpkgs { inherit system; }; }); -in { + forAllSystems = fn: + nixpkgs.lib.genAttrs allSystems + (system: fn { pkgs = import nixpkgs { inherit system; }; }); + + createNixosSystem = { system, hostname, username ? "ephase" }: nixpkgs.lib.nixosSystem { + system = system; + specialArgs = { + inherit stateVersion inputs; + hostname = hostname; + username = username; + }; + modules = [ + ./nixos/default.nix + ]; + }; + + createHomeConfiguration = { system ? "x86_64-linux", hostname, username ? "ephase" }: + home-manager.lib.homeManagerConfiguration { + pkgs = import nixpkgs { + system = system; + overlays = [ nixgl.overlay ]; + }; + extraSpecialArgs = { + inherit stateVersion inputs; + hostname = hostname; + username = username; + }; + modules = [ + ./home-manager/default.nix + ]; + }; + in { devShells = forAllSystems ({ pkgs }: { default = pkgs.mkShell { name = "nixfiles"; @@ -46,71 +74,19 @@ in { }; }); nixosConfigurations = { - morty = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - specialArgs = { - inherit stateVersion inputs; - hostname = "morty"; - username = "ephase"; - }; - modules = [ - ./nixos/default.nix - home-manager.nixosModule - ]; - }; - mrmeeseeks = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - specialArgs = { - inherit stateVersion inputs; - hostname = "mrmeeseeks"; - username = "ephase"; - }; - modules = [ - ./nixos/default.nix - home-manager.nixosModule - ]; - }; - luci = nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - specialArgs = { - inherit stateVersion inputs; - hostname = "luci"; - username = "ephase"; - }; - modules = [ - ./nixos/default.nix - home-manager.nixosModule - ]; - }; + morty = createNixosSystem { system = "x86_64-linux"; hostname = "morty"; }; + mrmeeseeks = createNixosSystem { system = "x86_64-linux"; hostname = "mrmeeseeks";}; + luci = createNixosSystem { system = "x86_64-linux"; hostname = "luci"; }; }; homeConfigurations = { - rick = home-manager.lib.homeManagerConfiguration { - pkgs = import nixpkgs { - system = "aarch64-linux"; - overlays = [ nixgl.overlay ]; - }; - extraSpecialArgs = { - inherit stateVersion inputs; - hostname = "rick"; - username = "ephase"; - }; - modules = [ - ./home-manager/default.nix - ]; - }; - work = home-manager.lib.homeManagerConfiguration { - pkgs = import nixpkgs { - system = "x86_64-linux"; - overlays = [ nixgl.overlay ]; - }; - extraSpecialArgs = { - inherit stateVersion inputs; - hostname = "work"; - username = "yorick-barbanneau"; - }; - modules = [ - ./home-manager/default.nix - ]; + "ephase@rick" = createHomeConfiguration { system = "aarch64-linux"; hostname = "rick";}; + "ephase@luci" = createHomeConfiguration { system = "x86_64-linux"; hostname = "luci";}; + "ephase@morty" = createHomeConfiguration { system = "x86_64-linux"; hostname = "morty";}; + "ephase@mrmeeseeks" = createHomeConfiguration { system = "x86_64-linux"; hostname = "mrmeeseeks";}; + "yorick-barbanneau@work" = createHomeConfiguration { + system = "x86_64-linux"; + hostname = "work"; + username = "yorick-barbanneau"; }; }; }; diff --git a/home-manager/base.nix b/home-manager/base.nix index 13a4a20..2501587 100644 --- a/home-manager/base.nix +++ b/home-manager/base.nix @@ -1,13 +1,4 @@ -{ lib, hostname, ...}: +_: { - programs.home-manager.enable = true; - home.sessionPath = [ - "$HOME/.local/bin" - ]; - imports = [ - ../hosts/${hostname}/home-config.nix - ../modules/home-manager/default.nix - ] ++ lib.optional ( - builtins.pathExists ../hosts/${hostname}/includes/home-manager.nix - ) ../hosts/${hostname}/includes/home-manager.nix; + } diff --git a/home-manager/default.nix b/home-manager/default.nix index 6470228..befa969 100644 --- a/home-manager/default.nix +++ b/home-manager/default.nix @@ -1,12 +1,23 @@ -{ stateVersion, username, inputs, ... }: +{ inputs, lib, stateVersion, username, hostname, ... }: { - home.stateVersion = stateVersion; - home.username = "${username}"; - home.homeDirectory = "/home/${username}"; - fonts.fontconfig.enable = true; imports = [ inputs.sops-nix.homeManagerModules.sops - ./base.nix + ../hosts/${hostname}/home-config.nix ../nixos/includes/system/overlay.nix - ]; + ../modules/home-manager/default.nix + ] ++ lib.optional ( + builtins.pathExists ../hosts/${hostname}/includes/home-manager.nix + ) ../hosts/${hostname}/includes/home-manager.nix; + + nixpkgs.config.allowUnfree = true; + programs.home-manager.enable = true; + fonts.fontconfig.enable = true; + + home = { + inherit stateVersion username; + homeDirectory = "/home/${username}"; + sessionPath = [ + "$HOME/.local/bin" + ]; + }; } diff --git a/nixos/default.nix b/nixos/default.nix index 983345b..d8a2103 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -1,4 +1,4 @@ -{ inputs, pkgs, stateVersion, hostname, username, hostConfig, ... }: +{ pkgs, stateVersion, hostname, ... }: { imports = [ # Include the results of the hardware scan. ../hosts/${hostname}/hardware-configuration.nix @@ -15,11 +15,9 @@ ./includes/system/overlay.nix ]; - nixpkgs.config.allowUnfree = true; - - boot.kernelPackages = pkgs.linuxPackages_latest; +boot.kernelPackages = pkgs.linuxPackages_latest; networking.hostName = hostname; console = { @@ -30,32 +28,10 @@ useXkbConfig = true; # use xkbOptions in tty. }; - environment.systemPackages = with pkgs; [ git zsh ]; - home-manager = { - useGlobalPkgs = true; - useUserPackages = true; - extraSpecialArgs = { - inherit hostConfig; - inherit hostname; - }; - - # NixOS system-wide home-manager configuration - sharedModules = [ - inputs.sops-nix.homeManagerModules.sops - ]; - - users.${username} = { - home.stateVersion = stateVersion; - imports = [ - ../home-manager/base.nix - ]; - }; - }; - system.stateVersion = stateVersion; } diff --git a/taskfile.yaml b/taskfile.yaml index 02f28c8..282de94 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -17,19 +17,19 @@ tasks: sources: - "**/*.nix" - build:*: + nixos:build:*: vars: TARGET: "{{index .MATCH 0}}" cmds: - nixos-rebuild --flake .#{{.TARGET}} build - switch:*: + nixos:switch:*: vars: TARGET: "{{index .MATCH 0}}" cmds: - doas nixos-rebuild --flake .#{{.TARGET}} switch - test:*: + nixos:test:*: vars: TARGET: "{{index .MATCH 0}}" cmds: @@ -41,12 +41,6 @@ tasks: cmds: - home-manager build --flake .#{{.TARGET}} - home:test:*: - vars: - TARGET: "{{index .MATCH 0}}" - cmds: - - home-manager test --flake .#{{.TARGET}} - home:switch:*: vars: TARGET: "{{index .MATCH 0}}" From 44ce61ce0cd1119db2c2a6d616a37d595a7857de Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 9 Oct 2024 23:52:15 +0200 Subject: [PATCH 02/10] feat: check shell script with git hook --- lefthook.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lefthook.yaml b/lefthook.yaml index 76a8dd0..4410eea 100644 --- a/lefthook.yaml +++ b/lefthook.yaml @@ -8,11 +8,19 @@ commit-msg: run: cat {1} | convco check --from-stdin lint:typos: run: cat {1} | typos - + pre-commit: parallel: false commands: lint:typos:staged_files: run: typos {staged_files} + shellcheck: + glob: "*.sh" + run: shellcheck {staged_files} + shfmt: + glob: "*.sh" + run: shfmt -d -ln bash -i 4 {staged_files} + pre-push: parallel: false commands: From 7ef10b87e3659bdd84b9c14687c8feb9e3a819ff Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Thu, 10 Oct 2024 02:03:56 +0200 Subject: [PATCH 03/10] feat: improve Taskfile --- taskfile.yaml | 95 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/taskfile.yaml b/taskfile.yaml index 282de94..b8663df 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -2,47 +2,112 @@ version: "3" set: [errexit, pipefail, nounset] shopt: [globstar] +env: + GENERATION_IN_DAYS: 20d + tasks: check:sh: + desc: Check SH files, will call all check:sh:* tasks cmds: - - shellcheck **/*.sh + - task: check:sh:shellcheck + - task: check:sh:shfmt + check:sh:shellcheck: + cmds: + - find . -type f -name "*.sh" -exec shellcheck {} \; + sources: + - "**/*.sh" + check:sh:shfmt: + cmds: + - find . -type f -name "*.sh" -exec shfmt -d -ln bash -i 4 {} \; sources: - "**/*.sh" check:typos: + desc: Check typos in all files cmds: - typos . check:flake: + desc: Check flake cmds: - nix flake check sources: - "**/*.nix" - - nixos:build:*: + + nixos:*:*: + desc: Manage NixOS build, use nixos:verb:target format vars: - TARGET: "{{index .MATCH 0}}" + VERB: "{{index .MATCH 0}}" + TARGET: "{{index .MATCH 1}}" + preconditions: + - sh: '[[ {{.VERB}} = build || {{.VERB}} = test || {{.VERB}} = switch ]]' + msg: verb must be build, test or switch + cmds: + - task: nixos:{{.VERB}} + vars: + TARGET: "{{.TARGET}}" + + nixos:build: + internal: true cmds: - nixos-rebuild --flake .#{{.TARGET}} build - - nixos:switch:*: - vars: - TARGET: "{{index .MATCH 0}}" + requires: + vars: [TARGET] + + nixos:switch: + internal: true cmds: - doas nixos-rebuild --flake .#{{.TARGET}} switch + requires: + vars: [TARGET] - nixos:test:*: - vars: - TARGET: "{{index .MATCH 0}}" + nixos:test: + internal: true cmds: - doas nixos-rebuild --flake .#{{.TARGET}} test + requires: + vars: [TARGET] + + home:*:*: + desc: Manage Home-Manager configuration, use home:verb:target format + vars: + VERB: "{{index .MATCH 0}}" + TARGET: "{{index .MATCH 1}}" + preconditions: + - sh: '[[ {{.VERB}} = build || {{.VERB}} = switch ]]' + msg: verb must be build or switch + cmds: + - task: home:{{.VERB}} + vars: + TARGET: "{{.TARGET}}" home:build:*: - vars: - TARGET: "{{index .MATCH 0}}" + internal: true cmds: - home-manager build --flake .#{{.TARGET}} + requires: + vars: [TARGET] home:switch:*: - vars: - TARGET: "{{index .MATCH 0}}" + internal: true cmds: - home-manager switch --flake .#{{.TARGET}} + requires: + vars: [TARGET] + + generation-list:nixos: + cmds: + - nixos-rebuild list-generations + + generation-list:home: + cmds: + - home-manager generations + + gc:nixos: + desc: Garbage collect NixOS + cmds: + - doas nix-collect-garbage --delete-older-than {{.GENERATION_IN_DAYS}} + + gc:home: + desc: Garbage collect Home-Manager + cmds: + - home-manager expire-generations {{.GENERATION_IN_DAYS}} + - nix store gc From a8ce26fa9037259355163a82dc53b801fbcb535a Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Thu, 10 Oct 2024 02:05:32 +0200 Subject: [PATCH 04/10] feat: rename Taskfile --- taskfile.yaml => Taskfile.dist.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename taskfile.yaml => Taskfile.dist.yaml (98%) diff --git a/taskfile.yaml b/Taskfile.dist.yaml similarity index 98% rename from taskfile.yaml rename to Taskfile.dist.yaml index b8663df..404f325 100644 --- a/taskfile.yaml +++ b/Taskfile.dist.yaml @@ -79,14 +79,14 @@ tasks: vars: TARGET: "{{.TARGET}}" - home:build:*: + home:build: internal: true cmds: - home-manager build --flake .#{{.TARGET}} requires: vars: [TARGET] - home:switch:*: + home:switch: internal: true cmds: - home-manager switch --flake .#{{.TARGET}} From 2dee9fc6fbf99a2f43251d17e9302028b90a2ec3 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sat, 12 Oct 2024 19:09:44 +0200 Subject: [PATCH 05/10] feat(taskfile): improve targets management --- Taskfile.dist.yaml | 53 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/Taskfile.dist.yaml b/Taskfile.dist.yaml index 404f325..6980847 100644 --- a/Taskfile.dist.yaml +++ b/Taskfile.dist.yaml @@ -12,11 +12,13 @@ tasks: - task: check:sh:shellcheck - task: check:sh:shfmt check:sh:shellcheck: + desc: Pass Shellcheck on all *.sh files cmds: - find . -type f -name "*.sh" -exec shellcheck {} \; sources: - "**/*.sh" check:sh:shfmt: + desc: Pass shfmt in check mode on all sh files cmds: - find . -type f -name "*.sh" -exec shfmt -d -ln bash -i 4 {} \; sources: @@ -31,7 +33,30 @@ tasks: - nix flake check sources: - "**/*.nix" - + + host:*:update: + desc: Update package version then build NixOS and Home-Manager + vars: + TARGET: "{{index .MATCH 0}}" + cmds: + - nix flake update + - task: nixos:build:{{.TARGET}} + - task: home:build:{{.TARGET}} + preconditions: + - sh: '[[ -d ./hosts/{{.TARGET}} ]]' + msg: host `{{.TARGET}}` not found in `hosts/` directory + + host:*:switch: + desc: Switch both Home-Manager and NixOS generations + vars: + TARGET: "{{index .MATCH 0}}" + cmds: + - task: nixos:switch:{{.TARGET}} + - task: home:switch:{{.TARGET}} + preconditions: + - sh: '[[ -d ./hosts/{{.TARGET}} ]]' + msg: host `{{.TARGET}}` not found in `hosts/` directory + nixos:*:*: desc: Manage NixOS build, use nixos:verb:target format vars: @@ -66,6 +91,16 @@ tasks: requires: vars: [TARGET] + nixos:list-gen: + desc: List Nixos generations + cmds: + - nixos-rebuild list-generations + + nixos:gc: + desc: Garbage collect NixOS + cmds: + - doas nix-collect-garbage --delete-older-than {{.GENERATION_IN_DAYS}} + home:*:*: desc: Manage Home-Manager configuration, use home:verb:target format vars: @@ -93,21 +128,13 @@ tasks: requires: vars: [TARGET] - generation-list:nixos: - cmds: - - nixos-rebuild list-generations - - generation-list:home: + home:list-gen: + desc: List Home-Manager generations cmds: - home-manager generations - gc:nixos: - desc: Garbage collect NixOS - cmds: - - doas nix-collect-garbage --delete-older-than {{.GENERATION_IN_DAYS}} - - gc:home: - desc: Garbage collect Home-Manager + home:gc: + desc: Garbage collect Home-Manager packages cmds: - home-manager expire-generations {{.GENERATION_IN_DAYS}} - nix store gc From 8fd30ecb0ab3740042018945fce78662731a0dcc Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sat, 12 Oct 2024 22:22:00 +0200 Subject: [PATCH 06/10] chore: simplify home-manager target name --- flake.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index 4bc95ff..84149e0 100644 --- a/flake.nix +++ b/flake.nix @@ -79,11 +79,11 @@ luci = createNixosSystem { system = "x86_64-linux"; hostname = "luci"; }; }; homeConfigurations = { - "ephase@rick" = createHomeConfiguration { system = "aarch64-linux"; hostname = "rick";}; - "ephase@luci" = createHomeConfiguration { system = "x86_64-linux"; hostname = "luci";}; - "ephase@morty" = createHomeConfiguration { system = "x86_64-linux"; hostname = "morty";}; - "ephase@mrmeeseeks" = createHomeConfiguration { system = "x86_64-linux"; hostname = "mrmeeseeks";}; - "yorick-barbanneau@work" = createHomeConfiguration { + "rick" = createHomeConfiguration { system = "aarch64-linux"; hostname = "rick";}; + "luci" = createHomeConfiguration { system = "x86_64-linux"; hostname = "luci";}; + "morty" = createHomeConfiguration { system = "x86_64-linux"; hostname = "morty";}; + "mrmeeseeks" = createHomeConfiguration { system = "x86_64-linux"; hostname = "mrmeeseeks";}; + "work" = createHomeConfiguration { system = "x86_64-linux"; hostname = "work"; username = "yorick-barbanneau"; From 3e497c997a83093d0acf4ae496d5fb4da25f1c58 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sat, 12 Oct 2024 22:24:39 +0200 Subject: [PATCH 07/10] docs: update README --- README.md | 73 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a559b32..f8cce04 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,57 @@ -My NixOS configuration ----------------------- +# My NixOS configuration -My configurations for NixOS using flakes. For now only my desktop computer is -configured with this system. +My machines and dotfiles management using Nix / NixOS. -## Install +## Installation + +Project contains a devshell installing all needed and a +[Taskfile](https://Taskfile.dev) is available to help manage elements whether +Home-Manager or NixOS configuration. ``` -doas nixos-rebuild switch --flake ".#mrmeeseeks" +task --list-all +task: Available tasks for this project: +* check:flake: Check flake +* check:sh: Check SH files, will call all check:sh:* tasks +* check:sh:shellcheck: Pass Shellcheck on all *.sh files +* check:sh:shfmt: Pass shfmt in check mode on all sh files +* check:typos: Check typos in all files +* home:*:*: Manage Home-Manager configuration, use home:verb:target format +* home:gc: Garbage collect Home-Manager packages +* home:list-gen: List Home-Manager generations +* host:*:switch: Switch both Home-Manager and NixOS generations +* host:*:update: Update package version then build NixOS and Home-Manager +* nixos:*:*: Manage NixOS build, use nixos:verb:target format +* nixos:gc: Garbage collect NixOS +* nixos:list-gen: List Nixos generations ``` -## Update the flakes.lock - -``` -nix flake update -doas nixos-rebuild switch --flake ".#mrmeeseeks" -``` - -## Update manual packages from github - -Some modules use `fetchFromGithub` who need *SHA-256* hash to meet -reproductibility. Theses hashed can be obtain with `nix-prefetch-github`: +Managing *NixOS* installation, you can use all `nixos:*` targets for example: ```shell -$ nix-shell -p nix-prefetch-github -$ nix-prefetch-ghithub --rev v1.22.0 sindresorhus pure -{ - "owner": "sindresorhus", - "repo": "pure", - "rev": "87e6f5dd4c793f6d980532205aaefe196780606f", - "sha256": "TR4CyBZ+KoZRs9XDmWE5lJuUXXU1J8E2Z63nt+FS+5w=" -} +# build mrmeeseeks nixos: +task nixos:build:mrmeeseeks + +# build/switch nixos installation for mrmeeseeks +task nixos:switch:mrmeeseeks + +# build ephase home configuration for mrmeeseeks: +task home:build:mrmeeseeks + +# build / switch ephase home configuration for mrmeeseeks ``` -In this example we have obtained the hash for the specific 1.22.0 version. +## Update nixpkgs version + +Update packages repositories version can be done with `host::update`: + +``` +task host:mrmeeseeks:update +``` + +`flake.lock` file will be updated then both NixOS and Home-Manager will be +rebuilt. Note than new generation will not be activated, to do so you need to: + +``` +task host:mrmeeseeks:switch +``` From f113664131539f6a5732e816403ebeb1bd18092a Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 13 Oct 2024 11:30:08 +0200 Subject: [PATCH 08/10] refactor: rewrite `create_conf` script --- .../video/kdenlive/files/create_conf | 155 +++++++++++------- 1 file changed, 97 insertions(+), 58 deletions(-) diff --git a/modules/home-manager/video/kdenlive/files/create_conf b/modules/home-manager/video/kdenlive/files/create_conf index f0f929f..432f6a0 100755 --- a/modules/home-manager/video/kdenlive/files/create_conf +++ b/modules/home-manager/video/kdenlive/files/create_conf @@ -1,7 +1,20 @@ #!/usr/bin/env bash error() { - >&2 printf "\e[31mE\e[0m %s\n" "$1" + printf -v message "\e[31mERROR:\e[0m %s\n" "$1" + message="${message} \e[1mStack trace\e[0m:\n" + for (( i=1; i<${#FUNCNAME[@]}; i++ )) + do + if [[ $i = $((${#FUNCNAME[@]} - 1)) ]]; then + message="${message} └" + else + message="${message} ├" + fi + message="${message}─ source:\e[3;34m${BASH_SOURCE[$i]}\e[0m" + message="${message} function:\e[3;34m${FUNCNAME[$i]}\e[0m" + message="${message} line:\e[3;34m${BASH_LINENO[$i-1]}\e[0m\n" + done + >&2 printf "%b\n" "${message}" } die() { @@ -21,13 +34,18 @@ process_args() { shift ;; *) - break + conf="$*" + break esac shift done } mount_device() { + if [[ $# -lt 1 || $# -gt 2 ]]; then + error "mount_device need 1 or 2 arguments, $# provided : '$*'" + return 1 + fi local label="$1" local unmount="${2:-0}" local device @@ -60,6 +78,10 @@ mount_device() { } get_file_hash() { + if [[ ! $# -eq 2 ]]; then + error "get_file_size need 2 arguments, $# provided : '$*'" + return 1 + fi local file local file_size @@ -91,23 +113,31 @@ get_file_hash() { } move_file(){ + if [[ ! $# -eq 2 ]]; then + error "move_file need 2 arguments, $# provided : '$*'" + return 1 + fi local source="$1" local destination="$2" - printf "Move %s to %s:\n" "$source" "$destination" + printf "Move %s to %s\n" "$source" "$destination" if [[ ! -d "${destination%/*}" ]]; then error "$destination directory does not exists" return 1 fi if ! mv "$source" "$destination"; then - error "Error moving $source file, file must not exists" + error "Error moving $source file, file seems to not exists" return 1 fi return 0 } get_file_size() { + if [[ ! $# -eq 1 ]]; then + error "get_file_size need 2 arguments, $# provided : '$*'" + return 1 + fi local file file=$1 if [ -f "$file" ] @@ -119,84 +149,93 @@ get_file_size() { } insert_media() { + if [[ ! $# -eq 2 ]]; then + error "insert_media need 2 arguments, $# provided : '$*'" + return 1 + fi local filename filesize filehash tag filename="$1" tag="$2" - [ ! -f "$filename" ] && { - error "File $filename not found, can't insert media"; - return; - } - [ -z "$tag" ] && { - error "Tag not defined, can't insert media" - } + if [[ ! -f "$filename" ]]; then + error "File $filename not found, can't insert media" + return 1 + fi + if [[ -z "$tag" ]]; then + error "Tag not defined, can't insert media (filename: $filename)" + return 1 + fi filesize=$(get_file_size "$filename") filehash=$(get_file_hash "$filename" "$filesize") - sed -i -e "s#\[\[${tag}\]\]#${filename}#g" \ - -e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \ - -e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \ - "${folder}/montage.kdenlive" || { - error "sed command error on insert media!" - } + if ! sed -i \ + -e "s#\[\[${tag}\]\]#${filename}#g" \ + -e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \ + -e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \ + "${folder}/montage.kdenlive"; then + error "sed command error on insert media!" + return 1 + fi + return 0 } -base_dir="${HOME}/medias/videos/sambab" +function main() { + process_args "$@" -conf="$*" -key_label=${key_label:-SAMBAB} -recorder_label=${recorder_label:-"LS-100"} + base_dir="${HOME}/medias/videos/sambab" + key_label=${key_label:-"SAMBAB"} + recorder_label=${recorder_label:-"LS-100"} -printf "Creation de la conférence de %s\n" "$conf" -cd ~/medias/videos/sambab || exit 1 + printf "Creation de la conférence de %s\n" "$conf" + cd ~/medias/videos/sambab || exit 1 -#Get las friday date for creating folder -friday=$(date --date='last Friday' +"%Y.%m.%d") + #Get las friday date for creating folder + friday=$(date --date='last Friday' +"%Y.%m.%d") -#Add basedir, then I have my directory -folder="${base_dir}/${friday}-${conf// /_}" + #Add basedir, then I have my directory + folder="${base_dir}/${friday}-${conf// /_}" -# create it -mkdir "${folder}/"{rushes,audio,titles} -p || die "folder creation failed" + # create it + mkdir "${folder}/"{rushes,audio,titles} -p || die "folder creation failed" -cp "${base_dir}/logo_amis.svg" "${folder}/titles" || die "SVG copy failed!" -cp "${base_dir}/template.kdenlive" "${folder}/montage.kdenlive" || die "Template copy failed!" + cp "${base_dir}/logo_amis.svg" "${folder}/titles" || die "SVG copy failed!" + cp "${base_dir}/template.kdenlive" "${folder}/montage.kdenlive" || die "Template copy failed!" -inkscape_date=$(LC_ALL=fr_FR.UTF-8 date --date='last Friday' +"%A %d %B %Y") + inkscape_date=$(LC_ALL=fr_FR.UTF-8 date --date='last Friday' +"%A %d %B %Y") -printf "Put Name on title\n" -sed -i "s/\[\[conf\]\]/${conf//_/ }/g" "${folder}/titles/logo_amis.svg" || die "Change text on credits screen failed!" + printf "Put Name on title\n" + sed -i "s/\[\[conf\]\]/${conf//_/ }/g" "${folder}/titles/logo_amis.svg" || die "Change text on credits screen failed!" -printf "Put date on title\n" -sed -i "s/\[\[date\]\]/${inkscape_date}/g" "${folder}/titles/logo_amis.svg" || die "Change date failed!" + printf "Put date on title\n" + sed -i "s/\[\[date\]\]/${inkscape_date}/g" "${folder}/titles/logo_amis.svg" || die "Change date failed!" -printf "Export SVG title \n" -inkscape --export-type="png" "${folder}/titles/logo_amis.svg" || die "Export failed" + printf "Export SVG title \n" + inkscape --export-type="png" "${folder}/titles/logo_amis.svg" || die "Export failed" -printf "Importing PNG File in %s/montage.kdenlive \n" "$folder" -insert_media "$folder/titles/logo_amis.png" "logo" + printf "Importing PNG File in %s/montage.kdenlive \n" "$folder" + insert_media "$folder/titles/logo_amis.png" "logo" -## Get file from USB key -if mount_device "$key_label"; then - if move_file "/run/media/${USER}/${key_label}/RECORD/EXREC/"*.MP3 "${folder}/audio/audio.mp3"; then - insert_media "${folder}/audio/audio.mp3" + ## Get file from USB key + if mount_device "$key_label"; then + move_file "/run/media/${USER}/${key_label}/RECORD/EXREC/"*.MP3 "${folder}/audio/audio.mp3" + move_file "/run/media/${USER}/${key_label}/$(date --date='last Friday' +'%Y-%m-%d')"\ *.mkv "${folder}/rushes/obs.mkv" + mount_device "$key_label" 1 fi - if move_file "/run/media/${USER}/${key_label}/$(date --date='last Friday' +'%Y-%m-%d')"\ *.mkv "${folder}/rushes/obs.mkv"; - then - insert_media "${folder}/rushes/obs.mkv" video + ## Get audio file from RECORDER + if mount_device "$recorder_label"; then + move_file "/run/media/${USER}/${recorder_label}/RECORDER/FOLDER_A/"VOC_*.wav "${folder}/audio/recorder.wav" fi - mount_device "$key_label" 1 -fi -## Get audio file from RECORDER -if mount_device "$recorder_label"; then - if move_file "/run/media/${USER}/${recorder_label}/RECORDER/FOLDER_A/"VOC_*.wav "${folder}/audio/recorder.wav"; then - insert_media "${folder}/audio/recorder.wav" - fi - mount_device "${recorder_label}" 1 -fi + move_file "$HOME/downloads/$(date --date='last Friday' +'%Y%m%d')"_*.mp4 "${folder}/rushes/presensation.mp4" -printf "Change root folder in montage.kdenlive to %s\n" "$folder" -# sed -i -e "s#\[\[root_folder\]\]#${folder}#g" "${folder}/montage.kdenlive" + insert_media "${folder}/audio/audio.mp3" audio + insert_media "${folder}/rushes/obs.mkv" video + insert_media "${folder}/audio/recorder.wav" audio_recorder + insert_media "${folder}/rushes/presensation.mp4" video_intro + printf "Change root folder in montage.kdenlive to %s\n" "$folder" + sed -i -e "s#\[\[root_folder\]\]#${folder}#g" "${folder}/montage.kdenlive" +} + +main "$@" exit 0 From 5ff1e36a53809e2576857adb54844476377d1823 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 13 Oct 2024 11:41:53 +0200 Subject: [PATCH 09/10] chore: rename create_conf script then lefthook analyse it when commit --- .../home-manager/video/kdenlive/default.nix | 2 +- .../files/{create_conf => create_conf.sh} | 66 +++++++++---------- 2 files changed, 31 insertions(+), 37 deletions(-) rename modules/home-manager/video/kdenlive/files/{create_conf => create_conf.sh} (84%) diff --git a/modules/home-manager/video/kdenlive/default.nix b/modules/home-manager/video/kdenlive/default.nix index 267d79c..3073e0b 100644 --- a/modules/home-manager/video/kdenlive/default.nix +++ b/modules/home-manager/video/kdenlive/default.nix @@ -14,6 +14,6 @@ in frei0r inkscape ]; - home.file.".local/bin/create_conf".source = ./files/create_conf; + home.file.".local/bin/create_conf".source = ./files/create_conf.sh; }; } diff --git a/modules/home-manager/video/kdenlive/files/create_conf b/modules/home-manager/video/kdenlive/files/create_conf.sh similarity index 84% rename from modules/home-manager/video/kdenlive/files/create_conf rename to modules/home-manager/video/kdenlive/files/create_conf.sh index 432f6a0..e58df6f 100755 --- a/modules/home-manager/video/kdenlive/files/create_conf +++ b/modules/home-manager/video/kdenlive/files/create_conf.sh @@ -3,8 +3,7 @@ error() { printf -v message "\e[31mERROR:\e[0m %s\n" "$1" message="${message} \e[1mStack trace\e[0m:\n" - for (( i=1; i<${#FUNCNAME[@]}; i++ )) - do + for ((i = 1; i < ${#FUNCNAME[@]}; i++)); do if [[ $i = $((${#FUNCNAME[@]} - 1)) ]]; then message="${message} └" else @@ -12,7 +11,7 @@ error() { fi message="${message}─ source:\e[3;34m${BASH_SOURCE[$i]}\e[0m" message="${message} function:\e[3;34m${FUNCNAME[$i]}\e[0m" - message="${message} line:\e[3;34m${BASH_LINENO[$i-1]}\e[0m\n" + message="${message} line:\e[3;34m${BASH_LINENO[$i - 1]}\e[0m\n" done >&2 printf "%b\n" "${message}" } @@ -25,17 +24,18 @@ die() { process_args() { while :; do case $1 in - -l|--label) - key_label="$2" - shift - ;; - -r|--recorder) - recorder_label="$2" - shift - ;; - *) - conf="$*" - break + -l | --label) + key_label="$2" + shift + ;; + -r | --recorder) + recorder_label="$2" + shift + ;; + *) + conf="$*" + break + ;; esac shift done @@ -58,7 +58,7 @@ mount_device() { device="/dev/$(lsblk -o KNAME,LABEL | grep "${label}" | awk '{print $1}')" # check is we found a device with this name - if [[ $device = "/dev/" ]]; then + if [[ $device = "/dev/" ]]; then error "Can't get device named $label is device plugged?" return 1 fi @@ -87,19 +87,15 @@ get_file_hash() { file=$1 file_size=${2:-1} - if (( file_size != -1 )) - then - if [ -f "$file" ] - then - if (( file_size > 0 )) - then - if (( file_size > 2000000 )) - then + if ((file_size != -1)); then + if [[ -f "$file" ]]; then + if ((file_size > 0)); then + if ((file_size > 2000000)); then #calculate local tmp_file tmp_file=mktemp - head -c 1MB "$file" > "$tmp_file" - tail -c 1MB "$file" >> "$tmp_file" + head -c 1MB "$file" >"$tmp_file" + tail -c 1MB "$file" >>"$tmp_file" printf "%s" "$(md5sum "$tmp_file" | cut -d" " -f 1)" rm "$tmp_file" else @@ -112,7 +108,7 @@ get_file_hash() { printf "%s" -1 } -move_file(){ +move_file() { if [[ ! $# -eq 2 ]]; then error "move_file need 2 arguments, $# provided : '$*'" return 1 @@ -140,10 +136,9 @@ get_file_size() { fi local file file=$1 - if [ -f "$file" ] - then - wc -c < "$file" - return + if [[ -f "$file" ]]; then + wc -c <"$file" + return fi printf "%s" -1 } @@ -167,10 +162,10 @@ insert_media() { filesize=$(get_file_size "$filename") filehash=$(get_file_hash "$filename" "$filesize") if ! sed -i \ - -e "s#\[\[${tag}\]\]#${filename}#g" \ - -e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \ - -e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \ - "${folder}/montage.kdenlive"; then + -e "s#\[\[${tag}\]\]#${filename}#g" \ + -e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \ + -e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \ + "${folder}/montage.kdenlive"; then error "sed command error on insert media!" return 1 fi @@ -196,7 +191,6 @@ function main() { # create it mkdir "${folder}/"{rushes,audio,titles} -p || die "folder creation failed" - cp "${base_dir}/logo_amis.svg" "${folder}/titles" || die "SVG copy failed!" cp "${base_dir}/template.kdenlive" "${folder}/montage.kdenlive" || die "Template copy failed!" @@ -209,7 +203,7 @@ function main() { sed -i "s/\[\[date\]\]/${inkscape_date}/g" "${folder}/titles/logo_amis.svg" || die "Change date failed!" printf "Export SVG title \n" - inkscape --export-type="png" "${folder}/titles/logo_amis.svg" || die "Export failed" + inkscape --export-type="png" "${folder}/titles/logo_amis.svg" || die "SVG export failed" printf "Importing PNG File in %s/montage.kdenlive \n" "$folder" insert_media "$folder/titles/logo_amis.png" "logo" From 965122406c6de22fcf80ca112bbcbbc9bdb836a5 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 29 Sep 2024 15:14:08 +0200 Subject: [PATCH 10/10] feat: exclude lua files check from typo --- _typos.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_typos.toml b/_typos.toml index a44fcd3..42bfc5c 100644 --- a/_typos.toml +++ b/_typos.toml @@ -9,3 +9,7 @@ edn = "edn" [type.images] extend-glob = ["*.png"] check-file = false + +[type.lua] +extend-glob = ["*.lua"] +check-file = false